homepage Welcome to WebmasterWorld Guest from 54.226.173.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
VirtualHosts Not Working
jrote1




msg:4577984
 8:18 pm on May 26, 2013 (gmt 0)

Previously on my server I had one domain and the configuration was:


NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:*>
ServerName example.com

# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

UseCanonicalName Off

RewriteEngine On

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

ErrorDocument 403 http://www.example.com/

LogFormat "%V %h %l %u %t \"%r\" 5s 5b" vcommon
CustomLog logs/access_log vcommon

VirtualDocumentRoot /home/%-3/public_html
</VirtualHost>


Now I have purchased another domain and am trying to set it up and requests keep forwarding to www.example.com. What is going on? Thanks in advance. This is what i've currently got:


NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *.example.org:*>
ServerName example.org
ServerAlias example.org

SSLEngine on
SSLCertificateFile /etc/ssl/certs/www.example.org/example.org.crt
SSLCertificateKeyFile /etc/ssl/certs/www.example.org/example.key
SSLCertificateChainFile /etc/ssl/certs/www.example.org/gd_bundle-g2.crt

# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

UseCanonicalName Off

RewriteEngine On

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

ErrorDocument 403 http://www.example.org/

LogFormat "%V %h %l %u %t \"%r\" 5s 5b" vcommon
CustomLog logs/access_log vcommon

VirtualDocumentRoot /codeyourway/%-3/public_html
</VirtualHost>

<VirtualHost *.example.com:*>
ServerName example.com
ServerAlias example.com

# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

UseCanonicalName Off

RewriteEngine On

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

ErrorDocument 403 http://www.example.com/

LogFormat "%V %h %l %u %t \"%r\" 5s 5b" vcommon
CustomLog logs/access_log vcommon

VirtualDocumentRoot /home/%-3/public_html
</VirtualHost>


[edited by: tedster at 11:27 pm (utc) on May 26, 2013]

[edited by: Ocean10000 at 9:30 pm (utc) on May 27, 2013]
[edit reason] switch to example.com and example.org [/edit]

 

lucy24




msg:4578029
 11:50 pm on May 26, 2013 (gmt 0)

and requests keep forwarding to www.example.com

Oi! Tedster! Are you positive you did the right replaces in the right places?

Most of the time, changing domain names to example dot something is necessary in the Apache subforum because it's the only way people can see exactly what you typed. But here there's the risk that the wrong thing got changed in the wrong place. As I write this (see timestamp) it looks as if everything is forwarding to "example.com" because that's what you have told it to do. But it's impossible to know whether this is an editing mistake, or you simply forgot to change the domain names in the new config file.

Incidentally, as long as we're here: the optimal domain-name-canonicalization code goes

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

where "www.example.com" is the exact form of your preferred name. There are further nuances if you need to allow for subdomains. Either way, the form
^(.+\.)?
doesn't make a lot of sense (and is pretty ghastly as a Regular Expression ;))

Dideved




msg:4578044
 1:39 am on May 27, 2013 (gmt 0)

Either way, the form
^(.+\.)?
doesn't make a lot of sense


I can translate for you. ;)

It will match any subdomain. So, for example, if the requested host was sub.example.com, then OP's code won't redirect to www.example.com.

Dideved




msg:4578050
 2:02 am on May 27, 2013 (gmt 0)

@OP Is there any chance that there are htaccess files lingering around? I ask because looking at your config, I can't spot the cause of your issue. It looks like the example.org host redirects only to example.org, and the example.com host redirects only to example.com.

lucy24




msg:4578053
 2:27 am on May 27, 2013 (gmt 0)

I can translate for you.

DO NOT PATRONIZE ME.

^(.+\.)?
will not only match subdomains--presumably the intention of the person who wrote the code. It will also match the entire hostname, including port number if any. It will then require the server to go all the way back to the beginning once it figures out that when you said . you really meant [a-z]. Or possibly even [a-df-z]-- a more efficient approach if none of your subdomains happen to begin with the same letter as your domain name.

More to the point, the entire construct is superfluous: If you do have subdomains, you only need
example\.com$
omitting the opening anchor and the optional section. But unless you have vast numbers of wild-card subdomains, it is better to specify them by name.

Dideved




msg:4578073
 6:36 am on May 27, 2013 (gmt 0)

It will also match the entire hostname...


No, it won't, because it still has to match example\.com$ to be successful.

...It will then require the server to go all the way back to the beginning once it figures out that when you said


Ahh. So actually you're only worried about the performance of backtracking. Let me put your mind at ease by reminding you of the benchmark that revealed that whatever performance difference there may be is infinitesimally small. To add any complexity to our code for a micro-optimization is "ghastly" and "doesn't make a lot of sense." ;)

Also, just FYI, even [a-z] would require some backtracking. It would start off trying to match "example" as if it were a subdomain, then it would backtrack when it can't match the rest of the pattern. You're making a big fuss about backtracking but accomplishing so little. Don't sweat the little stuff. We don't need to worry about micro-optimizations.

More to the point, the entire construct is superfluous


We agree! :) I later noticed this too when I was reading the OP's code more closely. He doesn't need to filter out subdomains. He only needs to not match them in the first place. Which, incidentally, his first condition already accomplishes.

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