Subdomains & removing www.

Using a single web hosting account to host multiple sites

Subdomains & removing www.

Postby StijnH » Tue Jun 02, 2009 4:39 pm

mod_rewrite FAQ wrote:To remove www. from all sub domains and domains, put the following in a .htaccess in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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



no-www.org wrote:
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]



The setup:
root -> domains -> domain.tld -> public_html
public_html contains the .htaccess file and a folder named sub for sub.domain.tld

The result:
OK http://domain.tld/ stays http://domain.tld/
OK http://www.domain.tld/ goes to http://domain.tld/

OK http://sub.domain.tld/ stays http://sub.domain.tld/
NOT OK http://www.sub.domain.tld/ goes to http://sub.domain.tld/sub/
Expected behaviour http://www.sub.domain.tld/ goes http://sub.domain.tld/

I use DirectAdmin (well, my host uses it), and at the DirectAdmin forum I was advised to use separate .htaccess files for each subdomain. That shouldn't be necessary with this code, right?
I said I understand the workaround, but that the code does not behave as expected, and got this reply.
Subdomains have their own VirtualHost entries, so even if they share the domain.tld part and are located in a subdirectory, they are their "own entities" with their own webroot. That's why I wouldn't call domain.tld and sub.domain.tld having their own .htaccess rules exactly a workaround.


Does anyone here have any idea why the code is not behaving as expected?

Thank you :)
StijnH
 
Posts: 5
Joined: Tue Jun 02, 2009 4:25 pm

Postby richardk » Wed Jun 03, 2009 12:18 pm

This should work
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

%{REQUEST_URI} is from the original request so should not be "corrupted" by internal processing.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby StijnH » Wed Jun 03, 2009 3:49 pm

Works like a charm, thank you :)

So is the "corruption" from how subdomains are handled by DirectAdmin? Or would it be a server setting?
StijnH
 
Posts: 5
Joined: Tue Jun 02, 2009 4:25 pm

Postby richardk » Thu Jun 04, 2009 1:10 pm

How the sub domains are set up.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby StijnH » Thu Jun 04, 2009 2:50 pm

Subdomains are added via a menu, which creates the folder and DNS settings.

The DNS record of a subdomain looks like this
Code: Select all
www.sub 14400   IN   A   12.34.56.78


Unfortunately I do not have access to the VirtualHosts list.
Also, DirectAdmin is an expensive closed source packet so I cannot set up a test environment.
StijnH
 
Posts: 5
Joined: Tue Jun 02, 2009 4:25 pm

Postby richardk » Fri Jun 05, 2009 10:39 am

%{REQUEST_URI} is from the original request so should not be "corrupted" by internal processing.

Corrupted is the wrong word, unexpected is better. It would probably make sense if we knew the internal process.

Subdomains are added via a menu, which creates the folder and DNS settings.

The DNS record of a subdomain looks like this

The DNS is not part of the Apache processing, it just gets the request to the server.

Unfortunately I do not have access to the VirtualHosts list.
Also, DirectAdmin is an expensive closed source packet so I cannot set up a test environment.

Do you want to try and fix this? If you do you'd be better off asking the DirectAdmin support to find out how they process the request.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby StijnH » Sat Jun 06, 2009 12:46 pm

Ok, I've posted again at the DirectAdmin forums, hope this can be solved :)

Thank you for your help so far, richardk :D
StijnH
 
Posts: 5
Joined: Tue Jun 02, 2009 4:25 pm

Postby StijnH » Sat Jun 06, 2009 2:06 pm

Reply from the DirectAdmin forum

tillo wrote:Well, it's not really an issue. Using "$1" is wrong because the .htaccess file get parsed in the domain directory, not the subdomain one, so in RewriteRule the "^(.*)$" regular expression is "/subdir/..." instead of the original request "/...".

There is no "corruption by internal processing" like that poster said. "$1" doesn't work just because the subdomain resides in the directory of the domain, and .htaccess files are recursive. Apache tries to read any .htaccess file present in all the parent directories of any document root.

Using "%{REQUEST_URI}" to redirect a website is the only correct way, even if the backreference "$1" works most of the time.


So maybe the FAQ should be modified, so it says "%{REQUEST_URI}" ?
StijnH
 
Posts: 5
Joined: Tue Jun 02, 2009 4:25 pm

Postby richardk » Sat Jun 06, 2009 3:41 pm

They are correct. Changed.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 24 guests

cron