Forum Moderators: phranque

Message Too Old, No Replies

Proper 301 redirect via .htaccess

         

BaseballGuy

8:55 pm on Mar 4, 2012 (gmt 0)



The reason I'm asking this question is because I have heard that there are several ways of doing a 301 redirect via the .htaccess file. I'm still a bit confused.


This is for an ecommerce site.

The way I was taught it was:

RewriteRule ^greewidgets\.html /accessories-greenwidgets-c-2_13 [L,R=301]


....with the first page (greenwidgets\.html) being 301 redirected to the second page (/accessories-greenwidgets-c-2_13 [L,R=301])


I tried that, and it doesn't work. I'm on an Apache server Virtual Dedicated.


I also don't understand why there is a "\" before the ".html" in the first line of the rule.

I googled it and found several ways of doing it and got even more confused.

Thanks if you can help.

g1smd

9:06 pm on Mar 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The rule target should include the protocol and hostname.

Literal periods in Regular Expressions should be escaped. In RegEx, "
\.
" means "a period" and "
.
" means "ANY character".

For your rule, when a browser requests
example.com/greewidgets.html
the server returns a 301 response (instead of 200 or 404) and suggests that the browser makes a new request for
example.com/accessories-greenwidgets-c-2_13
instead.