This are my urls right nowm for my products
http://www.example.com/product.php?product=32723
I want to achieve this
http://www.example.com/product/32723-brand-model-productname
I have been modifying my .htaccess but really with no clue on how to achieve this.
You must match your URL with a RewriteRule pattern and rewrite it to the target URL
RewriteRule ^/product/(\d+)- /product.php?product=$1
This pattern matches any URL starting with /product/ and captures the following digits (\d+) followed by a dash -. The substitution URL will be /product.php?product= with the captured digits $1 appended.
To capture some part of the match, you enclose it in parenthesis (...). Read more on regular expressions used in mod_rewrite at Apache mod_rewrite Introduction - Regular Expressions.
This question is so common I just put up the whole answer with code here:
http://www.prescia.net/bb/coding/5-141018-simple_friendly-url
Related
I have a link like this
www.example.com/profile.php?name=sagar123
I used this rule:
RewriteRule ^profile/([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]
and now I can chang my URL to like this:
www.example.com/profile/sagar123
everything is fine but, now I want to use Hindi language characters also like this
www.example.com/profile.php?name=सागर (It's working fine)
www.example.com/profile/सागर (It is not working and showing Server error)
Please help me to write a rule or regex to accept all ([a-zA-Z0-9_-]+) and also Hindi Character.
Thanks and regards,
Hindi chars falls between \u0900-\u097F range. So you can use this inside character class.
To answer your question, most regexes(PCRE) do not support \u notation and support format of \x{900}
([\x{900}-\x{97F}a-zA-Z0-9_-]+)$
In python \u is supported, so :
([\u0900-\u097Fa-zA-Z0-9_-]+)$
see this for regex matching demonstrating both English and Hindi chars getting matched.
Also, see this for reading literal hindi char mapped to their hex values.
Use the (.*) regex class to match any type of character.
Also, you don't need the + operator at the end in your capturing ( and ) parens, as you're using ^ to indicate the beginning of the URL line, and $ to indicate its end, so a + greedy operator doesn't get you anything extra.
It should look like...
RewriteRule ^profile/(.*)$ profile.php?name=$1 [L]
If you need further info, I recommend taking a look at Apache.org: Apache mod_rewrite Introduction. They cover most of the characters I've discussed in this post up to this point: ., (, ), +, etc..
I'm trying to use the mod_rewrite module to create smooth URLs. So for example my example.com/pages/group/index.php?id=1&slug=example-keyword would become example.com/group/1-example-keyword.
The problem I'm having is with the second parameter and how it is split. As the second parameter uses dashes how could I fix this as at the moment it throws 404 errors.
.htaccess rule
RewriteRule ^group/([^-]*)-([^-]*)$ /pages/group/index.php?id=$1&slug=$2 [L]
Your regular expression explicitly prohibits dashes in the first and second groups.
Try this using . (any character) instead of [^-] (any character except -) in your second group:
RewriteRule ^group/([^-]*)-(.*)$ /pages/group/index.php?id=$1&slug=$2 [L]
In this expression, everything after group/ but before the first - will be captured in group 1, and everything after the first - will be captured in group 2.
I'm trying to write my .htaccess to support two vanity url's, the code will speak for itself as I'm not very good with .htaccess.
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?p=$1&s=$2 [L,QSA]
Upon going to for example http://website.com/home/test
I get a 404, but $_GET["p"] still returns back home if I go to just website.com/home.
Why am I getting a 404 when adding in my second variable in the url?
You get a 404 because /home/test does not match the expression ^[a-zA-Z0-9]+/?$. The second group after the first / exists beyond your $ string terminator. You need to add a second () group, which is optional. I have replaced the a-zA-Z0-9 character classes with [^/]+ which matches everything up to the next slash.
The (?:) indicates a non-capturing group encompasing the first /, with a capturing group () inside it to retrieve the $2 component. The entire construct is made optional with ? before the final $ terminator.
RewriteEngine On
RewriteRule ^([^/]+)(?:/([^/]+)/?)?$ index.php?p=$1&s=$2 [L,QSA]
I am using a regular expression to route my application, this is the regex I am using:
#/users/([a-zA-Z0-9\-\_]+)/posts#
But unfortunately it matches against these these urls too:
/users/:uid/posts/:pid
/users/:uid/posts/:pid/comment/:cid
But it shouldn't, it should match exact the same url so only:
/users/:uid/posts
What should I change in the regex to make it match the exact same string?
Thanks for help
You should include anchors for the beginning (^) and end ($) of the string:
#^/users/([a-zA-Z0-9\-\_]+)/posts/?$#
I also allowed for an optional / at the end of the URL.
So I have the following regular expression:
https?://(www\.)?flickr\.com/photos/(.+)/?
To match against the following URL:
http://www.flickr.com/photos/username/
How can I stop the final forward slash (/) from being included in the username sub-pattern (.+)?
I have tried:
https?://(www\.)?flickr\.com/photos/(.+?)/?
But then it only matches the first letter of the username.
https?://(?:www\.)?flickr\.com/photos/([^/]+)/?
I added ?: to the first group so it's not capturing, then used [^/] instead of the dot in the last match. This assures you that everything between "photos/" and the very next "/" is captured.
If you need to capture the first www just use this:
https?://(www\.)?flickr\.com/photos/([^/]+)/?
You need to make sure it doesn't match the forward slash:
https?://(?:www\.)?flickr\.com/photos/([^/]+)/?
You could also make the regex lazy (which is what I guess you were doing with the (.+?) syntax), but the above will work just fine
Change (.+) to ([^/]+). This will match until it encounters a /, so you might want to throw some other stuff in the class too.
There are generally two ways to do this:
Append a question mark, to make the matching non-greedy. .* will match as much as possible, .*? will match as little as possible.
Exclude the character you want to match next. If you want to stop on /, use [^/]*.
If you know there will be a trailing slash, take out the final ?.