.htaccess help please!

rugbyjack2005

Power Member
Joined
Oct 2, 2011
Messages
539
Reaction score
50
I have a new client that has recently moved their site to a new domain. While they have transferred most of their site to the new domain, they still have an application running on the old domain. This application has a login box with a bit of code in the .htaccess - see below:

AuthUserFile /folder/folder/auth/.htpasswd1
AuthGroupFile /dev/null
AuthName "Private"
AuthType Basic


require valid-user

Now i want to redirect the old site to the new site but with an exception that allows the application to still run on the old site. This is where i have got to so far but I am struggling with the exception part:

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


AuthUserFile /folder/folder/auth/.htpasswd1
AuthGroupFile /dev/null
AuthName "Private"
AuthType Basic


require valid-user



These two codes work individually but not together as i am missing the bit of exception code. Can someone fill in the missing bit of exception code please?

Thanks add rep will naturally be given to useful comments.
 
I would suggest that you try stackoverflow or serverfault for such a technical question.

Nevertheless, if your application has a certain string (footprint?!) in it you could try to use it as an exception like

RewriteCond %{QUERY_STRING} !applicationquerrystring
 
Try....

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder/folder/auth
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]

AuthUserFile /folder/folder/auth/.htpasswd1
AuthGroupFile /dev/null
AuthName "Private"
AuthType Basic
 
Remove the "L," from the rewrite rule line. That may solve your problem depending on the setup.

The alternative option is to add another condition to only rewrite when it doesnt match the URL you want to use.
 
Try....

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder/folder/auth
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]

AuthUserFile /folder/folder/auth/.htpasswd1
AuthGroupFile /dev/null
AuthName "Private"
AuthType Basic

Thanks. That seems to work on my test site. I'll have to wait till tomorrow to check for sure but it looks good so far
 
Thanks. That seems to work on my test site. I'll have to wait till tomorrow to check for sure but it looks good so far

Glad i could help, though i did miss off the "require valid-user" from my post which you will also need! And if you encounter any problems accessing the password protected pages with the new rules, you may need to add the following to your .htaccess

ErrorDocument 401 "Unauthorised"

EDIT - You may well find that you will need two seperate .htaccess files, one with the redirect in the root of your domain, and one with the auth directives and the ErrorDocument 401 "Unauthorised" line at the top in the folder that is to be protected.

Good luck :)
 
Last edited:
Ok so we have tried it now on the actual site and the redirect works but the application running off the old site doesn't. Any ideas? Is it possible just to redirect certain pages rather than the whole site?
 
Back
Top