You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Olivier Sannier <ob...@free.fr> on 2008/12/30 16:30:01 UTC

[users@httpd] Troubles enforcing canonical hostname in a .htaccess file

Hi all,

I have a website with an associated domain name hosted by a third party 
provider.
They let me place .htacess files in folders and mod_rewrite is enabled.
I would like that any request for "mydomain.com" be redirected to 
"www.mydomain.com" while letting through any request for other third 
level domains (such as test.mydomain.com or static.mydomain.com)
For the CMS that is handling the web content, I already have a 
RewriteRule that redirects any non existent folder or file to index.php, 
declared like this :

-------------------------------------------------------------------------------
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L,NC]
-------------------------------------------------------------------------------

I looked around and saw the example about canonical hostnames indicated 
inside the mod_rewrite guide here:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url

So I tried to adapt the given example and modified my .htacess file to this:

-------------------------------------------------------------------------------
RewriteEngine On

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

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L,NC]
-------------------------------------------------------------------------------

Unfortunately, this does not seem to work, requests made to 
http://domain.com/ are not redirected to http://www.mydomain.com/ but 
served directly by index.php, it's as if the first rule is not used at all.
I must be missing something obvious here, but I can't figure it right now.

Any help will be very much appreciated.

Regards
Olivier

---------------------------------------------------------------------
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] Troubles enforcing canonical hostname in a .htaccess file

Posted by Olivier Sannier <ob...@free.fr>.
Eric Covener wrote:
> On Tue, Dec 30, 2008 at 10:30 AM, Olivier Sannier <ob...@free.fr> wrote:
>   
>> So I tried to adapt the given example and modified my .htacess file to this:
>>
>> -------------------------------------------------------------------------------
>> RewriteEngine On
>>
>> RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
>> RewriteRule ^/(.*)$  http://www.mydomain.com/$1 [L,R=301]
>>     
>
> In .htaccess, the RewriteBase including a trailing slash are stripped
> before the comparison
> RewriteRule (.*)  http://www.mydomain.com/$1 [L,R=301]
Bugger, I missed that.
Thanks for pointing it out, it now works perfectly fine.

Cheers
Olivier

---------------------------------------------------------------------
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] Troubles enforcing canonical hostname in a .htaccess file

Posted by Eric Covener <co...@gmail.com>.
On Tue, Dec 30, 2008 at 1:09 PM, ernst schoen-rene <er...@gmail.com> wrote:
> The way to deal with this is never in the .htaccess file but in the virtual
> hosts configuration.  If you do not have access to this, you need to ask
> whoever does to make sure that www.domain.com and domain.com both go to the
> same virtual host.
>
> for instance, these lines in your httpd.conf file or httpd-vhosts.conf file
> would direct all urls ending in domain.com to one place
>
> <VirtualHost *:80>
>         ServerName domain.com
>         ServerAlias *.domain.com
> </VirtualHost>

OP wanted to force a canonical hostname, which this doesn't do.

-- 
Eric Covener
covener@gmail.com

---------------------------------------------------------------------
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] Troubles enforcing canonical hostname in a .htaccess file

Posted by ernst schoen-rene <er...@gmail.com>.
The way to deal with this is never in the .htaccess file but in the virtual
hosts configuration.  If you do not have access to this, you need to ask
whoever does to make sure that www.domain.com and domain.com both go to the
same virtual host.

for instance, these lines in your httpd.conf file or httpd-vhosts.conf file
would direct all urls ending in domain.com to one place

<VirtualHost *:80>
        ServerName domain.com
        ServerAlias *.domain.com
</VirtualHost>

On Tue, Dec 30, 2008 at 8:37 AM, Eric Covener <co...@gmail.com> wrote:

> On Tue, Dec 30, 2008 at 10:30 AM, Olivier Sannier <ob...@free.fr> wrote:
> >
> > So I tried to adapt the given example and modified my .htacess file to
> this:
> >
> >
> -------------------------------------------------------------------------------
> > RewriteEngine On
> >
> > RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
> > RewriteRule ^/(.*)$  http://www.mydomain.com/$1 [L,R=301]
>
> In .htaccess, the RewriteBase including a trailing slash are stripped
> before the comparison
> RewriteRule (.*)  http://www.mydomain.com/$1 [L,R=301]
>
> --
> Eric Covener
> covener@gmail.com
>
> ---------------------------------------------------------------------
> 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] Troubles enforcing canonical hostname in a .htaccess file

Posted by Eric Covener <co...@gmail.com>.
On Tue, Dec 30, 2008 at 10:30 AM, Olivier Sannier <ob...@free.fr> wrote:
>
> So I tried to adapt the given example and modified my .htacess file to this:
>
> -------------------------------------------------------------------------------
> RewriteEngine On
>
> RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
> RewriteRule ^/(.*)$  http://www.mydomain.com/$1 [L,R=301]

In .htaccess, the RewriteBase including a trailing slash are stripped
before the comparison
RewriteRule (.*)  http://www.mydomain.com/$1 [L,R=301]

-- 
Eric Covener
covener@gmail.com

---------------------------------------------------------------------
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