Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond Ignored

         

JanDoToDo

5:20 pm on May 17, 2010 (gmt 0)

10+ Year Member



Hey guys, I'm trying to use this code, but it seems as though the rewrite condition is never satisfied and so it doesnt work.


RewriteCond %{REQUEST_URI} ^payment
RewriteRule ^payment - [L]
RewriteCond $1 !^(index\.php|index\.html|assets|payment|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]


The first two lines I added just to play around but basically, any directory that isnt in the rewrite cond should be redirected to index.php. I am using a framework which requires this rewrite rule. The problem is I have a folder called payment in my document root which I want excluded from the rewrite rule.

In that folder, I have another htaccess file with the following code:


RewriteOptions Inherit

AuthName "Admin section"
AuthUserFile "/home/studentc/.htpasswds/public_html/assets/scripts/payment/passwd"
AuthType Basic
require valid-user


I need this folder to have basic authentication. When the auth lines are in the htaccess in the subdirectory the site loads, but i get a "page cannot be found" error when i try and access the payment folder (this is from my application not a standard, which means it is still trying to rewrite the URL). When I remove the authentication lines, the file in the payment directory is accessible and I dont get an error. I'm using Apache 2.0.63. If I put anything in the htaccess in the subdirectory, it is ignored indicating that the rewrite condition negating the payment folder is ignored as it never gets to that htacecss file.

Any help would be MUCH appreciated
Cheers,
J

g1smd

9:10 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whilst RewriteRule cannot see the leading slash of the path part of the incoming request, REQUEST_URI does begin with a leading slash.

As you have said, in your pattern, that the URI must begin with a p, it will never match - because the request itself always begins with a slash.

Before each RewriteCond add a couple of blank lines, and then add a
# comment
describing what each block of code should do.

JanDoToDo

9:38 pm on May 17, 2010 (gmt 0)

10+ Year Member



Thanks for your response. I changed:
RewriteCond %{REQUEST_URI} ^payment
to
RewriteCond %{REQUEST_URI} ^/payment

But it still doesnt work..? Any ideas how to make this work?

JanDoToDo

9:54 pm on May 17, 2010 (gmt 0)

10+ Year Member



I've just tried this:

RewriteCond %{REQUEST_URI} !^/(index\.php|index\.html|assets|payment|robots\.txt)(.*)$ [NC]
RewriteRule ^(.*)$ index.php/%1 [L]

But now it ALWAYS redirects to the index.php page regardless of the page you're on!

JanDoToDo

10:07 pm on May 17, 2010 (gmt 0)

10+ Year Member



IT has to be something to do with the fact that there is authentication in a htaccess in the subdirectory. Is there for some reason a conflict between these two things? i.e. basic authentication and rewrite rules? Its as thought because there is authentication on the folder, it over rules the rewrite statements and stops them from being executed?

jdMorgan

3:51 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, authentication takes precedence over mod_rewrite.

You may need to re-arrange files and directories to make this work, or move the code to a server config file, where you can use <Directory> containers to achieve fine-grained control.

You may also be able to apply "satisy any" using a combination of SetEnvIf testing Request_URI and Allow from env=varname. See Apache core (satisfy directive), mod_setenvif, and mod_access for more info.

Unfortunately, none of these methods are simple, as auth is pretty much always a big headache... :(

Jim