Forum Moderators: phranque
I've been working on this for two days... i'm going to kill myself !
My situation :
- a primary domain, let's say base-domain.com
- a subdomain, let's say example.com
I've a first htaccess in the root directory of example.com, that :
- redirect example.base-domain.com to example.com
- adds www
- adds a trailing slash
Here it is :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
##Adding trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
##Going to www.example.com
RewriteCond %{HTTP_HOST} ^example.base-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.base-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
And... it works fine :)
for example, http://example.base-domain.com/test goes to http://www.example.com/test/
Perfect !
The problem is when I try to secure a directory with htaccess authentification.
If I put the following classic htaccess in the "admin" directory :
AuthUserFile /home/xyz/xyz/admin/.htpass
AuthName "Identification obligatoire"
AuthType Basic
Require valid-user
I can't access http://www.example.com/admin/ : I get an error 301 (too many redirection).
If I remove the first (redirection) htaccess, the authentification works, but nothing is redirected.
If I remove the second (authentification) htaccess, the redirections work, but I can't access admin at all ! (it's a bit too secured).
So... is there a solution ?!?
THANK YOU
[edited by: jdMorgan at 3:33 pm (utc) on Nov. 10, 2009]
[edit reason] example.com [/edit]
Options +FollowSymLinks
RewriteEngine on
#
# Add missing trailing slash
RewriteCond $1 !\.htpass
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://www.example.com/$1/ [R=301,L]
#
# Force canonical www.example.com domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.base-domain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Externally redirect non-canonical hostname requests to single canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
An example pattern to exclude filepaths with periods in the final part would be "!^([^/]*/)*[^./]*\."
Note that this pattern would also exclude .htpass and .htaccess files, so the separate .htpass exclusion would not be required if you used this one.
Jim
I've made the changes you proposed, but I still have my authentification problem :(
If it can help, the real situation is here :
<snip>
And the reason why I add a trailing slash, is because by default, http://www.example.com/test goes to http://example.base-domain.com/test !
And, as I notices that it works with test/, I added this trailing slash trick.
thanx
[edited by: jdMorgan at 4:22 pm (utc) on Nov. 10, 2009]
[edit reason] No domains, e-mails or IMs, please. See TOS and forum charter. [/edit]
ErrorDocument 401 http://example.com/login-please.html
This is correct:
ErrorDocument 401 /login-please.html
Jim