Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect only when exact url matches?

I'm trying to redirect with apache .htaccess. I have the following codes

redirectMatch 301 /user http://clients.mydomain.com 

it works pretty well, but I don't want "/user/login" to be directed to "http://clients.mydomain.com/login".

How do I prevent it?

like image 974
Moon Avatar asked Nov 21 '11 19:11

Moon


2 Answers

Simple add a ^ to beginning and a $ to the end

^ tells tells the regex to match the beginning of the url

$ tells tells the regex to match the end of the url

redirectMatch 301 ^/user$ http://clients.mydomain.com 

So now your rule will only match /user and not /some/user or /user/name or /some/user/name


NOTE: If you want to match /user/ and /user then use ^/user/?$

? says to match the previous character/group zero to one times

like image 76
Jeff Wilbert Avatar answered Sep 23 '22 17:09

Jeff Wilbert


Use a regex, you're already using redirect match.

http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch

'$' matches the end of the url. In your example:

redirectMatch 301 ^/user/(.+)$ http://clients.example.com/ 
like image 24
caskey Avatar answered Sep 25 '22 17:09

caskey