Re: ServerAlias and RewriteRule

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 17. aug. 2011, at 15.36, Tom Evans wrote:
> On Wed, Aug 17, 2011 at 2:27 PM, Richard Taubo <ort@xxxxxxxxxxxx> wrote:
>> Hi!
>> 
>> I originally set my virtual host up like this in my httpd.conf file:
>> 
>> <snip>
>> 
>> Question 1)
>> Are there any problems running a):
>>        RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
>> Rather than b):
>>        RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
>> ?
>> Will e.g. a slash always be added as a part of $1 in a) if it exists in the
>> original url query (as caught by the RewriteCond shown above)?
> 
> All URLs start with a /. When you 'curl example.com', curl requests
> the URL '/' from the host 'example.com'. This is captured by your
> rewrite rule.
> So no, a slash is never added, the slash is captured from the original
> URL. You can either not capture the slash from the original URL, or
> leave off the / from the end of the rewritten URL, whichever makes you
> feel more comfortable.

Thanks!

So to be 110% clear, since this is kind of important to get right :-)

1) So either this – leave off the / from the end of the rewritten URL:
	RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]

2) Or this – not capture the slash from the original URL:
	RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301]

3) But not this – as was the alternative I started out with (the browsers I have tested
do not seem mind, but the rewrite logs shows that an extra slash is added):
	RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

>> 
>> Question 2)
>> Is the method I use to alias "example.com" with "www.example.com",
>> a good way to set up a ServerAlias in my httpd.conf file, or are there better ways?
>> My current method, as mentioned above, is:
>>    ServerName www.example.com
>>    ServerAlias example.com
>>    RewriteEngine On
>>    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
>>    RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
>> 
> 
> It's fine. Some people prefer to have the host name canonicalization
> occur in a separate vhost, as this separates the configuration for the
> 'correct' hostname from the configuration for 'incorrect' hostnames.

So instead of:
<VirtualHost *:80>
   ....
   ServerName www.example.com
   ServerAlias example.com
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
   RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
</VirtualHost>

They would rather create two VirtualHosts like this instead:
<VirtualHost *:80>
   ....
   ServerName www.example.com
   ....
</VirtualHost>

<VirtualHost *:80>
   ....
   ServerName example.com
   RewriteEngine On
   RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
   ....
</VirtualHost>


Appreciate your answers!

Richard Taubo
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
   "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx




[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux