Jump to content

Forcing www.example.com to example.com with mass hosting


cameronjdavis

Recommended Posts

I'm running Apache 1.3 and have lots of sites. Currently I am rewriting using the URL with HTTP_HOST to map to the correct document root.

 

RewriteEngine On

RewriteMap  lowercase  int:tolower

RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$

RewriteRule  ^/(.*)$  /.../www/${lowercase:%2}/$1

 

I'd like to add the ability for www.hostname.com to redirect to hostname.com. I need help.

Link to comment
Share on other sites

Something like?...

 

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

 

or, more generically?...

 

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

 

2nd rule tested OK on local machine (www.localhost.com/somefolder)

 

#Useful hosts entry for debugging...
127.0.0.1       www.localhost.com
127.0.0.1       localhost.com

 

Link to comment
Share on other sites

It didn't have the desired affect. I may be using rewrites incorrectly.

See the problem live in action at http://my.comfypage.com/koalas/

 

RewriteEngine On

RewriteMap  lowercase  int:tolower

#the next two lines are what you suggested

RewriteCond %{HTTP_HOST} ^www\.(.*)\..*$ [nc]

RewriteRule (.*) http://%1.com$1 [L,R]

#my own document root mapping

RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$

#map request to a path that includes the address

RewriteRule  ^/(.*)$  /.../www/${lowercase:%2}/$1

Link to comment
Share on other sites

Sorry, yes. The example code was only for www.*.com only to avoid problems which need explaining!.

 

Making the first RewriteCond bracket optional (?) as you have - introduces a potential infinite loop bug. Since, once rewritten "www.this.com" to "this.com", "this.com" will STILL match your rule and will be applied over and over. (I tested this on my local machine and found it to be true). Remove the "?" since, if you have the correct URL you want the rule to be ignored anyway since it doesn't need rewriting.

 

If you want to wildcard vary both the domain prefix (MY.*.*) AND the top-level domain  - TLD - (say .COM .NET. ORG, .INFO etc.) then it is slightly more complex. You can even force many TLDs to one primary TLD if you wanted in the same rule.

 

Note also that case *shouldn't* really matter for the domain name (as opposed potentially to path names). However this may depend on what you've done elsewhere with other existing rules. If you have introduced case dependency into a rule heirachy then you'd probably have to check that.

So - wWw.WWw.cOM should = WWW.www.coM etc. as far as a user is concerned. Thus I never bother correcting case on domain names and leave them as input by the client rather than consume CPU time.

 

See below - Using 3 "greedy" operators (.*) in the regex (.com TLD only). Take care omitting the 3rd bracketed backref or you might find unintended matches happening if you bunch everything as one. I'd prefer to be more explicit+verbose and avoid unintended and unexpected bugs and interactions with other rewrites later on.

 

RewriteEngine On
#Force redirect to .COM TLD remove SD prefix
RewriteCond %{HTTP_HOST} ^(.*\.)(.*)\.com$ [nc]
RewriteRule (.*) http://%2.com$1 [L,R]

 

RewriteEngine On
#Uses 3 greedy operators - not ideal
#Force redirect removing SD prefix to original TLD
RewriteCond %{HTTP_HOST} ^(.*\.)(.*)\,(.*)$ [nc]
RewriteRule (.*) http://%2.%3$1 [L,R]

 

or (including the dot in the last backref)

 

RewriteEngine On
#Uses 3 greedy operators - not ideal
#Force redirect removing SD prefix to original TLD
RewriteCond %{HTTP_HOST} ^(.*\.)(.*)(\..*)$ [nc]
RewriteRule (.*) http://%2%3$1 [L,R]

 

If you can afford to specify a more limited range of both subdomain prefixes and TLD postfixes then you could remove 2 greedy matches and use bracketed ors. perhaps like (www|my|other)  and (org|com|info|co\.uk)

 

RewriteEngine On
#Force redirect - TLD remove SD prefix - match COM,ORG,NET only and redirect to original TLD
RewriteCond %{HTTP_HOST} ^(.*\.)(.*)\.(com|org|net)$ [nc]
RewriteRule (.*) http://%2.%3$1 [L,R]

 

Force a remap all TLDs to .com (everything else other than COM, ORG, NET is ignored)

 

RewriteEngine On
#Force redirect - TLD remove SD prefix - match COM,ORG,NET only and redirect all to .COM
RewriteCond %{HTTP_HOST} ^(.*\.)(.*)\.(com|org|net)$ [nc]
RewriteRule (.*) http://%2.com$1 [L,R]

 

I'm sure a super-regex guru would find a far more efficient way to do this though.

Apologies for any typoes - I didn't get time to test every rule live but I'm sure you should get the idea ;)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.