1

I want to set up 301 redirects in my .htaccess file so URLs like

http://example.com/Foo

http://example.com/Foo/Bar

http://example.com/Foo/Bar/Blah

change to

http://example.com/products/foo

http://example.com/products/foo/bar

http://example.com/products/foo/bar/blah

There are a discrete number of "Foo" cases which I can target with RewriteRule ^Foo, but how to append the "products" part?

anubhava
  • 761,203
  • 64
  • 569
  • 643
hitfactory
  • 2,896
  • 5
  • 24
  • 22

1 Answers1

1

First add this line in <VirtualHost> section OR at the end of your httpd.conf file:

RewriteMap lc int:tolower

Then have these rules in .htaccess file:

RewriteEngine on
Options +FollowSymlinks -MultiViews  
RewriteRule ^(Foo.*)$ /products/${lc:$1} [R=301,L]
  • R=301 for sending back 301 to browser
  • L for marking it last rule
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This works but I have to specify the hostname like so `RewriteRule ^Foo(.*)$ http://example.com/products/foo$1 [R=301,L,NC]` Also I end up with `http://example.com/products/foo/Bar` Is there another rule I should run to convert to lowercase before running this rule? – hitfactory Apr 21 '11 at 06:08
  • If you are not redirecting to a different host then you won't need `http://example.com` in the rules above. And for lowercasing I modified rules above, please try them. – anubhava Apr 21 '11 at 06:28
  • Rewrite Map in .htaccess causes 500 Internal Server Error. Does that need to go in httpd.conf instead? – hitfactory Apr 21 '11 at 06:31
  • Oh sorry, should have made it clear. Yes that line goes into `` section of `httpd.conf`. Please see this answer: http://stackoverflow.com/questions/2923658/convert-to-lowercase-in-a-mod-rewrite-rule for more. Or simply add this line `RewriteMap lc int:tolower` at the end of `httpd.conf` file. – anubhava Apr 21 '11 at 06:35
  • No probs. I have to get someone else to tweak httpd.conf later as it's not my server but looks like it will work. Thanks! – hitfactory Apr 21 '11 at 06:40