Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Code Confusion

Which one really works? Or do they all work?

         

icedowl

8:49 pm on Mar 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am currently using the following code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The results I've seen don't appear to be what I'm looking for. For example, with the above code, if I just type example.com in my address bar, I'm not seeing the "www." being added.

I have also tried this one:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

and I've tried this one:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www.example.com/ [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]


I am not sure that any of these are exactly correct. I've tried reading info at the Apache site and have simply come away more confused. Could someone help me get one that actually works? I thank you in advance.

jdMorgan

9:43 pm on Mar 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two of those code snippets are fairly close to correct, in that they should work (change the address bar). But all do different things. The first two are for use in .htaccess, while the last is for use in httpd.conf.

The first and third redirect requests for any domain or subdomain NOT matching the canonical domain, while the second is intended to redirect a specific non-canonical domain to the canonical domain. The second snippet has a fatal flaw, however, in that hostnames don't contain a "/", so it won't work.

If none work, then I'd expect you to be seeing errors in your server error log; Anything there?

Here is the code, cleaned-up and documented:


# For use in document-root .htaccess
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If request Host header is non-blank (HTTP/1.0 requests don't send this header and can't be redirected based on it)
RewriteCond %{HTTP_HOST} .
# And if requested domain is NOT the canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
# Redirect to requested page in canonical domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


# For use in document-root .htaccess
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If non-canonical domain requested (case-insensitive compare)
RewriteCond %{HTTP_HOST} ^example\.com [NC]
# Redirect to requested page in canonical domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


# For use in httpd.conf or conf.d
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If requested domain is NOT the canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
# And if request Host header is NOT blank (HTTP/1.0 requests don't send this header and can't be redirected based on it)
RewriteCond %{HTTP_HOST} !^$
# Redirect to same page in canonical domain
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]

Most of these clean-ups are subtle and just eliminate unnecessary processing. You must be careful with the [NC] flag to be sure the rule is doing what you really want it to do, and hostnames DO NOT end with "/". An [R] flag without a redirect type given will default to a 302, so specify [R=301] or [R=permanent] if that is what you want. With mod_rewrite, everything is in the details -- There's very little wiggle-room for 'making it up as you go.'

If none of these work, then I'd suspect that you have a server configuration problem, or that mod_rewrite is not available to you on that host.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

icedowl

3:50 am on Mar 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim, thank you for your help.

The second snippet has a fatal flaw, however, in that hostnames don't contain a "/", so it won't work.

Sorry. That was just a typo... but thanks for the heads-up on that.

No, I've not had any server errors by trying those codes. I've just not felt that they were entirely correct since the results weren't as expected. Mod-rewrite is definitely available. I'll give your first version a try.

icedowl

4:04 am on Mar 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks once again! That first code works like a charm.