简体   繁体   中英

htaccess redirect failed when using htpasswd authentication

I have a problem which is bugging me for over a day now, and I really have no idea where to look.

I have a website set up with a sub directory called cms , which is using a basic htpasswd login. All requests to the main site are redirected to index.php using htaccess, except requests to the cms -folder, images and so on.

This results in the following htaccess in www_root :

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

RewriteCond %{REQUEST_URI}  !^/cms
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|favicon\.ico|\.svg|\.pdf|\.ino)$ [NC]
RewriteCond %{REQUEST_URI}  !^/robots.txt$ [NC]
RewriteCond %{REQUEST_URI}  !^/BingSiteAuth.xml$ [NC]
RewriteCond %{REQUEST_URI}  !^/apple-app-site-association$ [NC]
RewriteRule (.*)  framework/index.php [L]

Inside the cms -folder is a separate htaccess, containing only the authentication stuff:

AuthName "CMS"
AuthUserFile /path/to/.htpasswd
AuthGroupFile /dev/null
AuthType Basic

require valid-user

The problem is, when I request cms/index.php , the request gets redirected to framework/index.php instead of cms/index.php , like RewriteCond %{REQUEST_URI} !^/cms isn't in the main htaccess, except when I remove cms/.htaccess , in which case everything is acting normal and I'm able to reach the (now unprotected) CMS.

The site is running on a Redhat Linux server, using Apache 2.4.16 and PHP 5.6.

Try turning on debug logging to watch what's going on:

RewriteLog "/tmp/debug.log"
RewriteLogLevel 6

As you step through the URLs on the site, the log will show you what rules are matching, and why.

For example when I set it up and watched /tmp/debug.log , I could see that this request ( /somedirectory ) was rewritten:

(4) RewriteCond: input='/somedirectory' pattern='!^/cms' => matched
(2) rewrite '/somedirectory' -> 'framework/index.php'
(2) local path result: framework/index.php

and this request ( /cms ) was not rewritten:

(2) init rewrite engine with requested uri /cms/index.php
(3) applying pattern '(.*)' to uri '/cms/index.php'
(4) RewriteCond: input='/cms/index.php' pattern='!^/cms' => not-matched
(1) pass through /cms/index.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM