homepage Welcome to WebmasterWorld Guest from 174.129.76.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Removing "www" from my domain but for dozens of domain.
herculano

5+ Year Member



 
Msg#: 4246839 posted 12:40 am on Dec 29, 2010 (gmt 0)

Hi guys,

newbie here for this section of the WebmasterWorld site.

I have a problem that I'm trying to solve.. say we have
a software that drives multiple domains but we wanted
to make sure that all domains are formatted without the "www"
this is what we are going for:

www.mydomain1.com -> mydomain1.com
...
www.mydomain100.com -> mydomain100.com

and also
www.mysite1.net -> mysite1.net
...
www.mysite100.net -> mysite100.net

this is what I have in my htaccess seems to work properly.. so far.. not sure if there's any potential problem that we didn't account for that's not obvious... any recommendation or validation is great!

RewriteEngine on
# check to make sure it's a www
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.(com|net)) [NC]
# if that's the case, redirect to HTTP_HOST only
RewriteRule (.*) http://%1/$1 [R=301,L]


best regards,
Thanks!

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4246839 posted 2:35 pm on Dec 31, 2010 (gmt 0)

There's a potential for competitive abuse here that's easy to fix if that is of any concern:

RewriteEngine on
#
# if requested hostname starts with "www"
RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.(com|net))\.?(:[0-9]+)?$ [NC]
# redirect to hostname without "www."
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

And to support both HTTP and HTTPS:

RewriteEngine on
#
# if requested hostname starts with "www."
RewriteCond %{SERVER_PORT}s->%{HTTP_HOST} ^(443(s)|[0-9]+s)->www\.([^.:]+\.(com|net))\.?(:[0-9]+)?$ [NC]
# redirect to hostname without "www.", preserving requested HTTP/HTTPS protocol
RewriteRule ^(.*)$ http%2://%3/$1 [R=301,L]

Note that the "->" subpattern is arbitrary. Although it implies concatenation to the reader's eye, it has no special meaning to mod_rewrite or regular expressions; its only purpose is to serve as a "soft anchor" to facilitate unambiguous parsing of the combined server variable strings.

Finally, if you might use additional subdomains at some point in the future, and want to redirect requests for hostnames like "forum.www.example.com" or "mobi.www.example.com", then you can remove those www's with:

RewriteEngine on
#
# if requested hostname contains "www."
RewriteCond %{SERVER_PORT}s->%{HTTP_HOST} ^(443(s)|[0-9]+)->([^.:]+\.)*www\.(([^.:]+\.)+(com|net))\.?(:[0-9]+)?$ [NC]
# redirect to hostname without "www.", preserving requested HTTP/HTTPS protocol
RewriteRule ^(.*)$ http%2://%3%4/$1 [R=301,L]

Jim

Brett_Tabke

WebmasterWorld Administrator brett_tabke us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4246839 posted 4:46 pm on Dec 31, 2010 (gmt 0)

ouch. Took 30 minutes to study that and I still don't get this line:

RewriteCond %{SERVER_PORT}s->%{HTTP_HOST} ^(443(s)|[0-9]+)->([^.:]+\.)*www\.(([^.:]+\.)+(com|net))\.?(:[0-9]+)?$ [NC]

What does the "s->" do?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4246839 posted 5:51 pm on Jan 5, 2011 (gmt 0)

Breaking this down:

What does the "->" do?

It does nothing, except to allow for easy parsing of the 'boundary' between the two concatenated variable values. That is, it serves only as a 'soft anchor' for use by the regex pattern.

Any non-Web-transmissible character (i.e. characters that the HTTP protocol RFC requires to be URL-encoded or prohibits from use in the specific URL-part being tested) or any rarely-occurring string could be used. I just use "<->" or "->" or ">" because it visually implies concatenation. :)

> What does the "s" in "%{SERVER_PORT}s" do?

It 'preloads' the "s" to be appended to 'http' in the substitution URL if the incoming request is made on port 443. If the request is made on any other port, then that "s" is not matched-into/defined as the $2 back-reference and it will get dropped, thus we always redirect https requests to https, and http requests to http.

It's just a complex-looking way to do a simple thing efficiently, since other solutions require two consecutive rules.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved