Forum Moderators: phranque

Message Too Old, No Replies

Prefix by default

         

Dexie

8:50 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm trying yo ensure that if anyone puts just the http or just the www, that it will still go to [sitename.com...] but when I try the code below, it doens't work, am I missing a bit somewhere pease?

Any help much appreciated.

Sev.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sitename.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.sitename.com$
RewriteRule ^(.*)$ [sitename.com...] [R=301,L]

jdMorgan

2:10 am on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like that code will loop forever to me. With the second RewriteCond and the RewriteRule, you are redirecting www.example.com to www.example.com.

Don't end-anchor your HOST names. Otherwise, an appended port number will break your code.

If I missed your point, please tell us what you mean by "it doesn't work."

Jim

Dexie

8:32 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Hi Jim,

By doesn't work, I meant that it just loops forever ;-)

What is the bit that I need to change please?

Any help appreciated.

Sev.

Dexie

8:51 am on Feb 23, 2005 (gmt 0)

10+ Year Member



Hi again Jim,

Just to clarify what I'm trying to attempt, assuming it is possible ;-)

I'm trying to do it, so that if someone inputs:

[sitename.com...]

or

www.sitename.com

or

sitename.com

they are always redirected via a 301 redirect to:

[sitename.com...]

I'm on an Apache server and have access to the .htaccess file.

I've been struggling with this for a few weeks and would really appreciate a hand on it.

Sev.

jdMorgan

6:01 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is really a pretty simple problem. At least since it's looping, we know that mod_rewrite is working -- that's often the hard part.

Understand that the user has no control over "entering" the "http://". If the user does not enter that, then the browser will add it automatically. The request *must* have a protocol specified, such as http, https, or ftp, before any connection to your server is possible. Therefore, the only case you have to worry about is a missing "www.". Since you tried to redirect www.sitename.com to www.sitename.com, your server looped doing redirects until the maximum number of allowed redirects was reached, either by your server or by your browser.

This should be all you need:


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

The "\" before the period is required to denote a literal period. Otherwise it will be handled as a regular-expressions token meaning, "any single character."

See our charter [webmasterworld.com] for links to useful references on mod_rewrite and regular expressions.

Jim

Dexie

9:48 am on Feb 24, 2005 (gmt 0)

10+ Year Member



Many thanks Jim, works a treat, I obviously got some bits wrong!

I have another post to make in a bit, in a new thread on this forum, hopefully I'll get some good help on that one as well - I have been through the threads and charter on it, but this next one hasn't been covered anywhere as far as I can tell.

Sev.