News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Rewritecond & non-www domains redirect

Started by MultiformeIngegno, June 17, 2010, 06:32:13 AM

Previous topic - Next topic

MultiformeIngegno

Hello! :)
I use this code to redirect my users from the domain with www to the one WITHOUT www. Example: www.website.com -> website.com:

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


It works flawlessly, however I noticed that it doesn't work with subdomains, for example forum.website.com is always accessible via www.forum.website.com! How can I change my code to redirect also my subdomains to the ones without www (www.xxxx.website.com -> xxxx.website.com)?

Thanks in advance! ;)
RockCiclopedia (wiki - forum), Tutta la storia del rock, scritta da voi ...
Rimanere aggiornati sul mondo della musica grazie al nuovo feed "RockCiclopedia Music News"!

Angelde93

I can choose with www or without www when I create a subdomain.

MrPhil

Quote from: MultiformeIngegno on June 17, 2010, 06:32:13 AM
It works flawlessly
No way. Your code redirects website.com to website.com. Did you mean the first line to be
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
?

QuoteHow can I change my code to redirect also my subdomains to the ones without www
I don't know why people insist on putting "www." in front of a subdomain -- takes all kinds, I guess. If you don't mind explicitly listing all your subdomains, you should be able to rewrite them:
RewriteCond %{HTTP_HOST} ^www\.xxxx\.website\.com [NC]
RewriteRule (.*) http://xxxx.website.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.yyyy\.website\.com [NC]
RewriteRule (.*) http://yyyy.website.com/$1 [R=301,L]

etc. That should work.

I'm not sure if there's a foolproof way to capture the subdomain (xxxx, yyyy) in the RewriteCond statement and then use it in the RewriteRule.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.website\.com [NC]
should capture the subdomain name, but I don't know if that's going to be $1 or something else on the RewriteRule, or if it's even available. You could experiment, you could ask your host, or you could look in the Apache .htaccess URL rewriting documentation.

MultiformeIngegno

Quote from: MrPhil on July 01, 2010, 10:16:38 PM
Quote from: MultiformeIngegno on June 17, 2010, 06:32:13 AM
It works flawlessly
No way. Your code redirects website.com to website.com. Did you mean the first line to be
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
?
Of course you're right.. sorry! :P

What do you think of this method instead (for subdomains)? :)

RewriteEngine on
# Externally redirect to remove "www" prefix from subdomains
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.website\.com [NC]
RewriteRule ^(.*)$ http://%1.website.com/$1 [R=301,L]
RockCiclopedia (wiki - forum), Tutta la storia del rock, scritta da voi ...
Rimanere aggiornati sul mondo della musica grazie al nuovo feed "RockCiclopedia Music News"!

MrPhil

The documentation on %1 is a bit hard to find, but yes, that looks like it should contain the xxxx or yyyy extracted in the previous line's pattern. It's worth a try, and if it doesn't work, you have the way I gave to fall back on.

Advertisement: