I need to redirect some link with or without trailing slashes:

www.domain.con/foo   → www.domain.com/redirect (working)
www.domain.com/foo/www.domain.com/redirect (not working)

I tried this rule in .htaccess:

RewriteRule (.*)/foo/$ http://www.domain.com/redirect$1 [L,R=301]

To redirect www.domain.com/foo or www.domain.com/foo/ to www.domain.com/redirect you need to make the match on the trailing slash optional with a question mark (/?):

RewriteRule ^foo/?$ http://www.domain.com/redirect [L,R=301]
  • Same problem, don't work – hellb0y77 Sep 30 '14 at 13:30
  • To resolve partially i have make a 301 redirect that work without trailing slash Redirect 301 /foo http://www.domain.com/redirect but with trailing slash and RewriteRule i have a double / es. http://www.domain.com/redirect// – hellb0y77 Sep 30 '14 at 13:43
  • That's very odd. I've just ran a test on an Amazon EC2 instance and it does work. How are you testing? I used wget -O /dev/null --no-cache <url> to make sure that my browser wasn't caching anything. The above command will show the 301 redirect. – garethTheRed Sep 30 '14 at 14:46
  • i tried to move RewriteRule from .htaccess to /etc/httpd/conf.d/myconf.conf inside <Virtualhost> directive and my original rules works (RewriteRule (.*)/foo/$ http://www.domain.com/redirect$1 [L,R=301]). It's possible that RewriteRule works only in a vitrualhost directive? – hellb0y77 Sep 30 '14 at 16:15
  • From the Apache documentation, RewriteRule works in server config, virtual host, directory, .htaccess – garethTheRed Sep 30 '14 at 19:16
RewriteRule ^foo/?$ http://www.domain.com/redirect [L,R=301]

This is a correct solution but will not carry the variable over, just a page to page. If you are having caching issues I recommend using https://httpstatus.io/ to verify the redirects as it doesn't seem to cache any of the redirect information. Also, testing using 302 instead of 301 will keep the browser from being stubborn about giving up the cached page location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.