.htaccess 301 redirect with regular expressions
43,484
Try using rewrite rules if you have apache:
RewriteEngine on
RewriteRule ^/users/(.*)$ http://www.example.com/profiles/$1 [R=301,L]
Note that you will need ModRewrite installed and enabled in your apache config. Pulled from here if you need a method for IIS.
Related videos on Youtube
Author by
macek
Updated on September 17, 2022Comments
-
macek almost 2 years
If I have:
redirect 301 /users/foo http://www.example.com/profiles/foo redirect 301 /users/bar http://www.example.com/profiles/bar
Can I do something like?
redirect 301 ^\/users/(.+)$ http://www.example.com/profiles/$1
Edit
Found a solution:
RedirectMatch users/(.+) http://www.exapmles.com/profiles/$1 [R=301,L]
This actually redirects instead of rewriting.
Edit 2
See @Darth Android's solution with
RewriteEngine
which works just as well :) -
macek about 14 yearsI actually wanted to redirect, not just rewrite. Appreciate the help though :)
-
user1984103 about 14 years@macek I haven't tested it personally, but I'm under the impression that will redirect with a 301 code. I'll play around with it a bit. The
[R=301,L]
means to stop processing rewrite rules and issue a 301 redirect. -
macek about 14 yearsah, I didn't know you could use
[R=301,L]
at then end of aRewriteRule
. Thanks for this :) -
user1984103 about 14 years@macek I didn't either until about 30 seconds ago. This is why I come to this website to be entirely honest. Thanks for increasing my knowledge! :P
-
rinogo over 6 yearsI believe the first
/
on theRewriteRule
needs to be removed for this to work as desired. (I had to remove it for it to work on my system.) With the first/
removed, we're left with:RewriteRule ^users/(.*)$ http://www.example.com/profiles/$1 [R=301,L]
-
Rob D. A. almost 6 yearsGuys, if I could ask as I'm trying to fix my weird changing url. For some time trying to do it but it just won't work. From time to time my Magento is changing URL which today is something_909.html then few days later it is no more something_909.html but something_911.html How can I do it with regex?