Forum Moderators: phranque

Message Too Old, No Replies

301 redirect to “canonical” URL

.htaccess code check

         

Lobo

2:11 am on Feb 10, 2006 (gmt 0)

10+ Year Member



This is a new site, but thought I'd setup the htaccess to redirect the mydomain.com to www.mydomain.com

It seems to be working fine, but just wanted to run the code by you to double check I'm doing the right thing..

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Redirect /htaccess-url-redirect.html http://www.mydomain.com/
</IfModule>

does this look fine?

And is this the right thing to do?

[edited by: jdMorgan at 5:33 am (utc) on Feb. 10, 2006]
[edit reason] De-linked. [/edit]

jdMorgan

5:33 am on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> This is a new site, but thought I'd setup the htaccess to redirect the mydomain.com to www.mydomain.com
This is indeed the best time to do it -- before you have a problem.

1) Your <IfModule mod_rewrite.c> container includes a Redirect directive, which is not included in mod_rewrite.
2) Not sure why you'd want the [NC] flag on the first RewriteCond...
3) Because you have mixed mod_rewrite and mod_alias directives, their order of execution cannot be controlled within this code -- This will be controlled by the reverse LoadModule order on Apache 1.x, or by the module priority in Apache 2.x.

Jim

Lobo

6:51 am on Feb 10, 2006 (gmt 0)

10+ Year Member



Thanks for that mate... except the code is copied and your directives don't make much sense to me ..

I did initially use

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

but it produced an error on my site and locked out access...

Found this other piece of code and it seems to work fine, at least does the change I want...

I would be happy to use a tighter / more sensible piece of code if you would recommend that..

You assistance would be apprieciated..

[edited by: jdMorgan at 2:43 pm (utc) on Feb. 10, 2006]
[edit reason] De-linked. [/edit]

jdMorgan

2:49 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



did initially use

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

but it produced an error on my site and locked out access...


That indicates a possible server configuration problem then, as this code has no errors in it. You could try that again without the Options line if you want to find out. If it fails both with and without the Options, then contact your hosting provider for assistance.

the code is copied and your directives don't make much sense to me ...

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

Lobo

3:04 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



Thanks for that information Jim ...

Does this mean that I should not be using the code that I have installed?

jdMorgan

3:58 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it means that you have a "mystery" which you may or may not wish to investigate before committing to hosting a site with this host.

The two samples of code are similar in behaviour, except that your first-posted code redirects *anything except* the canonical domain to the canonoical domain, while the second sample redirects only the non-canonical domain to the canonical domain. Which you use depends largely on your future plans; If you intend to add a bunch of subdomains later, you might want to use the second example. If however, you don't plan to add more subdomains, the first-posted code (less the NC flag) would catch more non-canonical domain variants.

A more correct form for your first example would be:


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>
Redirect /htaccess-url-redirect.html http://www.example.com/

This removes the Redirect directive from the <IfModule mod_rewrite.c> container, since it doesn't logically belong there. Another way to do it, using only mod_rewrite directives, would be:


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^htaccess-url-redirect\.html(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>

Jim

Lobo

5:40 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



Thanks jim ..

Using the first option gives the error ..

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, sysadmin@greatserversdns2.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

--------------------------------------------------------------------------------

Apache/1.3.33 Server at example.com Port 80

the <IfModule mod_rewrite.c> option works fine?

jdMorgan

7:19 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More information about this error may be available in the server error log.

If you can find that request, or do another request to create a fresher entry in the error log file, it will likely tell you exactly what's wrong.

Jim