i have a regular expression as below
http://www.abc.com/signup.php?id=2
RewriteRule ^signup/([a-z]+)/([a-z]+)$ /signup.php?$1=$2 [L]
but this is not working? can any one help me.is there any thing to enable in the .htaccess file?
As you are passing also numbers, you should match them in regular expression as well:
RewriteRule ^signup/([a-z0-9]+)/([a-z0-9]+)$ /signup.php?$1=$2 [L]
You might want to include other chars as well or rather use metaclass such as \w.
After edits, try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^signup\.php$ /signup/%1 [L,R=301]
RewriteRule ^signup/([0-9]+)$ /signup.php?id=$1 [L]
That'll redirect /signup.php?id=123 to /signup/123 which will then be rewritten back to /signup.php?id=123 (but not redirected).
Related
I have two rewrite rules for my application:
The first rule is a rule for /chef/index.php:
/chef/name -> /chef/?id=1234
The second rule is a rule for /recipes/index.php:
/r/name/nice-name-for-recipe ->
/recipes/?id=1234&nice_name=nice-name-for-recipe-name
The two rules work separately, but if I enabled both of them:
the first chef rule does not work,
the second recipes rule seems to work.
I tried to swap the order of the rules but I still cannot make both of them work.
Rules:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/chef/$ $1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /chef/index.php?id=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/r/$ $1/$2 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /recipes/index.php?id=$1&nice_name=$2 [QSA]
In the first rule, you tried to match ^([a-zA-Z0-9_-]+)/chef/$.
/chef/$ means it matches a url that ends with /chef/,
because $ means the end of string, e.g.:
http://anything.dev/chef/
So it does not match /chef/name/, it matches /chef/.
Similarly, your second rule does not match /r/name/nice-name-for-recipe,
it matches /r/$.
These rules just tell apache to fallback to static files.
It is useful if you need to serve static files,
but they are unrelated to this question.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
So your rules are roughly just:
RewriteRule ^(.*)$ /chef/index.php?id=$1 [QSA]
RewriteRule ^(.*)$ /recipes/index.php?id=$1&nice_name=$2 [QSA]
Now it is clear that why these rules work separately but only the second one works if you put them together.
They both matches ^(.*)$, that is every url (except those urls for static files).
Thus when putting them together, only the second one wins.
So the real rule effect is:
/chef/name ->
/chef/index.php?id=/chef/name&nice_name=
/r/name/nice-name-for-recipe ->
/chef/index.php?id=/r/name/nice-name-for-recipe&nice_name=
P.S. I think the deep causes of this question are:
You try to write regular expressions without understanding them.
The regular expression syntax is hard to understand. Specifically, $ is both used as pattern and variable prefix.
The index.php code is dirty. It should not accept urls blindly. If index.php errors out, the two rules will not seem to work. Dirty code is hard for detecting and locating problems, and insecure (attackers can construct dangerous special urls).
Your IDE is not smart enough to warn you against RewriteRule ^(.*)$ /recipes/index.php?id=$1&nice_name=$2 [QSA] since $2 is unset.
You're writing that you want to transform dev.website.com/chef/name into dev.website.com/chef/?id=1234. This cannot work as the id in the target URL doesn't exist in the source URL, so you need to think about how you want to include the ID in the original URL as well.
Once you sorted that out, I'd recommend you to read up more on regular expressions to fix the mod_rewrite rules.
Here's a great resource for testing and explaining regular expressions: https://regex101.com/
I'm very new to regular expression and I need to do a redirecting on my .htacess for some urls
Some examples are:
/lentes-de-contato/9/lentes-de-contato-biofinity-coopervision
/lentes-de-contato/9/lentes-de-contato-biofinity-teste
/lentes-de-contato/9/lentes-de-contato-biofinity
/lentes-de-contato/9/biofinity
The regex needs to match the word biofinity but don't match the word coopervision. I tried several ways to build an expression that looks like this: "biofinity" AND !"coopervision" but nothing seems to work. Till now I just have:
/lentes-de-contato/([0-9]+)/(.*biofinity.*)
Could anyone help me?
Maybe, so. Rewrterule will be achieved with your condition
RewriteCond %{REQUEST_URI} biofinity
RewriteCond %{REQUEST_URI} !coopervision
RewriteRule
You can do this in RewriteRule itself using negative lookahead:
RewriteRule ^lentes-de-contato/(\d+)/(?!.*?coopervision).*?-biofinity ... [L,NC]
hiii ,
I want to rewrite the directory www.mysite.com/index.php?category=news
to www.mysite.com/news
I write this code but its doesn't work
anyone can help, please
thanks for help
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?category=$1
Thanks for all who answered my question , all the codes are work when i try to write www.mysite.com/news , but I mean when i click on a link
"a href='index.php?category=news'"link"/a" I want to be rewrite to www.mysite.com/news immediately
Just to confirm, the pattern you want to match is all letters, numbers, hyphens and forward-slashes, right?
If so, try this
RewriteRule ^([a-zA-Z0-9/-]+)$ index.php?category=$1 [QSA,L]
I think the problem may have been your ordering of the hyphen and forward-slash in the character class expression. To match hyphens, they should appear first or last in the set of characters.
Does this work?
RewriteBase /
RewriteRule (.*) index.php?category=$1 [QSA,NC,L]
try this
Options +FollowSymLinks
RewriteEngine on
RewriteRule news/ index.php?category=news
RewriteRule news index.php?category=news
RewriteRule ^(news|or|some|other|category)$ index.php?category=$1 [QSA,NC,L]
I'm using URL rewriting with Wamp but I don't find the correct regex for my needs.
I'd like to transform http://localhost/site_artisans/site_artisans/peintre-annecy.php in http://localhost/site_artisans/site_artisanspeintre-annecy.php (remove the slash between site_artisans/site_artisans and whatever is after).
I thought of :
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/.* site_artisans/site_artisans [L]
(Unknown number of characters before and after and slash removed).
But this doesn't work.
Try
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/(.*) site_artisans/site_artisans$1 [L]
I tested it on http://martinmelin.se/rewrite-rule-tester/ and I believe the result is what you want.
Try this:
RewriteEngine on
RewriteRule .*site_artisans/(site_artisans/.*) site_artisans/$1 [L]
It's a part of my htaccess:
RewriteRule ^post/(.*)$ post.php?name=$1
RewriteRule ^(.*)/$ cat.php?name=$1
The URI will be somthing like this:
www.domain.com/category-name/
www.domain.com/post/hello-world
as you see, in the end of the first address (category) there is a '/' and on the second there isn't '/', how can I do it too on the second address? if I will do somthing like this:
RewriteRule ^post/(.*)/$ post.php?name=$1
it won't work because the server 'thinks' that I mean to category address.
hope you understand thank you.
Use the [L] modifier on every rule so it stops processing further rules when a rule matched. In this case you can even make the trailing slash optional!
RewriteRule ^post/(.*)$ post.php?name=$1 [L]
RewriteRule ^(.*)/?$ cat.php?name=$1 [L]
Try using (.*?) instead of (.*).
Without the question mark, it's "greedy" in that it will match everything up to the final match on the line. With the question mark, it will only match up to the first match on the line.
Make the slash optional (both times):
RewriteRule ^post/(.*?)/?$ post.php?name=$1 [L]
RewriteRule ^(.*?)/?$ cat.php?name=$1 [L]