Forum Moderators: phranque

Message Too Old, No Replies

.htaccess help with permanant redirects

         

hotchilidamo

4:10 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I want to redirect domainname.co.uk to www.domainname.co.uk

I've entered the following code into my htaccess but when I visit domainname.co.uk/contact.html for example it doesn't appear to redirect in my browser.

Here's what I've got:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.co.uk [NC]
RewriteRule (.*) http:// www.mydomain.co.uk/$1 [L,R=301]
ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
ErrorDocument 500 /error.html
Redirect /productpage.html http:// www.mydomain.co.uk/newproductpage.html
Redirect /productpage2.html http:// www.mydomain.co.uk/newproductpage2.html
...

Note: I have put a deliberate space between http and www just to make sure it displays ok in this forum

Have I got the order wrong? Is the syntax wrong? or is it all wrong?

DailyAmerican

4:18 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



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

By the way your rewrite rule looks there's a space b/w http:// and www.

hotchilidamo

5:49 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



Thanks, but that still doesn't appear to work

Is there something I should place at the bottom of the file?

Also, the space was deliberate just so it displays ok on the forum

DailyAmerican

5:54 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



Try using R=permanent

You can also try some of these.


redirectMatch 301 ^(.*)$ [domain.com...]
redirectMatch permanent ^(.*)$ [domain.com...]

redirect 301 /index.html [domain.com...]
redirect permanent /index.html [domain.com...]
redirectpermanent /index.html [domain.com...]

jdMorgan

6:42 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ... but that still doesn't appear to work

How, specifically, didn't it work?

Did you completely flush (delete) your browser cache before testing new code?

This should work fine:

ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
ErrorDocument 500 /error.html
#
RewriteEngine on
#
RewriteRule ^productpage\.html$ http://www.example.co.uk/newproductpage.html [R=301,L]
RewriteRule ^productpage2\.html$ http://www.example.co.uk/newproductpage2.html [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
#
# - or better, if you have no subdomains other than "www" -
#
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]

Note that the most-specific redirects are first to avoid multiple "chained" redirects on requests for your two obsolete URLs if the domain is also wrong.

If you use mod_rewrite for any functions, then use it for all, otherwise you cannot control the execution order of mod_alias versus mod_rewrite, and can have redirect-chaining problems as well as exposing internally-rewritten filepaths as URLs.

I do not recommend the use of a single error document for all errors: Each error document should be very specific, and be intended to inform and assist the visitor. 404 and 410 error pages should explain the problem and provide links to your home page, site search facility, and major category pages, as appropriate. All error documents (and especially the 403 error document) should have very few or zero external dependencies. That is, they should not reference or include any other images, stylesheets, external JavaScript files, etc. If you have such external dependencies, the chance for self-inflicted-denial-of-service increases.

Jim

hotchilidamo

3:15 pm on Feb 4, 2010 (gmt 0)

10+ Year Member



Thaks for your help Jim, just to clarify when I say it still doesn't work I mean that the problem I identified orignally still exists i.e. domainname.co.uk/contact.html doesn't redirect to www... in my browser.

Yes I have tried this after flushing the cache

Amended to your version but still no joy.

jdMorgan

4:45 pm on Feb 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to look at your server's set-up using FTP or a shell login. Make sure that there is not a separate file-directory set up for www versus non-www.

Related to that possibility is that your .htaccess code must be located in a directory which will be traversed for requests to either hostname.

Those are the only scenarios I can think of where the code wouldn't work, but also would not generate a 500-Server Error.

One thing you may be able to try (when your traffic is at minimum levels -- say, late at night) is to intentionally inject a syntax error into your .htaccess code -- change the spelling on one rule to "RewriteFool" for example. Then test to see that you get server errors when trying to access *both* hostnames. If not, it would prove that your code is *not* located where it will be executed for requests for both hostnames.

Jim