You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Geoff Soper <ge...@alphaworks.co.uk> on 2006/11/08 23:56:38 UTC

[users@httpd] Rewrite problem (adding www.)

Hi,
I use the following piece of code in an .htaccess file at 
/var/www/vhosts/domain.com/httpdocs/.htaccess to get traffic from 
http://domain.com onto http://www.domain.com which works perfectly:

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

However when the root of the site in question isn't at the top level of 
the domain (the following in an .htaccess file at 
/var/www/vhosts/domain.com/httpdocs/sub/.htaccess):

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

it fails in a certain case which leads to a request for 
http://domain.com/sub (note no trailing bracket) being served 
http://www.domain.com/sub//var/www/vhosts/domain.com/httpdocs/sub 
instead of http://www.domain.com/sub or http://www.domain.com/sub/

Can someone suggest how I make the rewrite condition / rule more robust?

Thanks,
Geoff


---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Rewrite problem (adding www.)

Posted by Joshua Slive <jo...@slive.ca>.
On 11/8/06, Geoff Soper <ge...@alphaworks.co.uk> wrote:
> Hi,
> I use the following piece of code in an .htaccess file at
> /var/www/vhosts/domain.com/httpdocs/.htaccess to get traffic from
> http://domain.com onto http://www.domain.com which works perfectly:
>
> RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
> RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,NE]
>
> However when the root of the site in question isn't at the top level of
> the domain (the following in an .htaccess file at
> /var/www/vhosts/domain.com/httpdocs/sub/.htaccess):
>
> RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
> RewriteRule ^(.*)$ http://www.domain.com/sub/$1 [R=301,L,NE]
>
> it fails in a certain case which leads to a request for
> http://domain.com/sub (note no trailing bracket) being served
> http://www.domain.com/sub//var/www/vhosts/domain.com/httpdocs/sub
> instead of http://www.domain.com/sub or http://www.domain.com/sub/
>
> Can someone suggest how I make the rewrite condition / rule more robust?

Use the RewriteBase directive or use %{Request_URI} explicitly in the
rewriterule.  (Or, even better, if you have access to httpd.conf, put
the RewriteRules in there.)

Joshua.

---------------------------------------------------------------------
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@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org