Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite add www problem

Having www added to url creates odd loop

         

bretw

5:54 pm on Feb 4, 2005 (gmt 0)

10+ Year Member



I'm trying to use mod_rewrite in my .htaccess file for subdomains, to add www to a request for the main site (and it's files), and to add a trailing slash.

Here is the file:
----------------START .htaccess-----------------
directoryindex index.php
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html

<Limit GET PUT POST>
deny from 216.***.105.94
deny from 66.***.40.10
deny from 213.**.128.0/17
</Limit>
Options -Indexes

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ [%{HTTP_HOST}...]

RewriteCond %{HTTP_HOST}!^www\.
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]

RewriteCond %{HTTP_HOST} kd.example.com
RewriteCond %{REQUEST_URI}!kd/
RewriteRule ^(.*)$ kd/$1 [L]
.
.
.
RewriteCond %{HTTP_HOST} mo.example.com
RewriteCond %{REQUEST_URI}!mo/
RewriteRule ^(.*)$ mo/$1 [L]
----------------END .htaccess-----------------

The problem I'm having is with subdirectories that are password protected using .htaccess (not the above .htaccess, obviously). If I enter an incorrect password it prompts me for the password again.. everything looks okay. But sometimes, when I enter the correct password it immediately directs me to the 401 error page. It is highly dependent on the syntax of the URL.

For example:
Using username: test
Using password : test
[example.com...] - loads fine
[example.com...] - 401 error
<snip>

I think there's a nasty loop happening here somewhere.

Can someone PLEASE point out the err of my ways?!

Thanks for any help!
Bret

[edited by: jdMorgan at 9:19 pm (utc) on Feb. 4, 2005]
[edit reason] Removed specifics, please see TOS. [/edit]

jdMorgan

9:37 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bret,

Welcome to WebmasterWorld!

I'd like to ask that you review the WebmasterWorld Terms of Service, and the Charter for this forum. The links are at the bottom center and top left of this page, respectively.

The main problem that I see with this code is that your syntax for {REQUEST_URI} is wrong. You must specify the leading slash in order to get a match. So, your loop-prevention is not working now. You should also escape the literal periods in the hostnames used in your {HTTP_HOST} checks.


RewriteEngine On
#
# Redirect adding trailing slash if missing
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
#
# Redirect adding leading www to domain or any subdomain if missing
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Redirect subdomains to subdirectories
RewriteCond %{HTTP_HOST} kw\.example\.com
RewriteCond %{REQUEST_URI} [b]!/k[/b]w/
RewriteRule ^(.*)$ /kw/$1 [L]
.
.
.
RewriteCond %{HTTP_HOST} mo\.example\.com
RewriteCond %{REQUEST_URI} [b]!/m[/b]o/
RewriteRule ^(.*)$ /mo/$1 [L]

Note that the second rule of the code --both your original and this modified version-- will add "www" to the beginning of your domain name and all of your subdomain names as well. If you only want to add www to your root domain name, then use this:

# Redirect adding leading www to root domain if not subdomain specified
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Let's fix these problems first, and then look at the auth problem if it still exists.

Jim

bretw

2:51 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Thanks for the help! Most things seem to be working fine now, except that if I try to authenticate to http://example.com/test I have to reauthenticate to http://www.example.com/test because the www rewrite is happening after the authentication. Is there any way to avoid this?

<snip>

[edited by: jdMorgan at 8:59 pm (utc) on Feb. 7, 2005]
[edit reason] Removed specifics. See TOS. [/edit]

jdMorgan

8:57 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code is fine. It is a fact that Apache authentication has higher priority, and that www.example.com is a different authentication domain than example.com.

There are several possible work-arounds, depending on how your site is structured. For example, bring people into the site on a public page, redirect to the other domain if necessary, and then authenticate.

The easiest cure is to control your inbound and internal links, and make sure you get the ones that point to the wrong domain fixed as soon as possible.

Jim