getting loops with this

New to mod_rewrite? This is a good place to start.

getting loops with this

Postby oldgit » Wed Aug 26, 2009 2:11 pm

Beginner here though I have a reasonable idea of what I'm doing

I have this in my htaccess and most of the time it works fine but every now and then I seem to get a loop - any ideas what is wrong

its doing rewrite to hhtps, friendly URLS, match to links from old site and error pages

is there a condition that is not exiting?

(also I'm not sure if I need the RewriteBase entry)
(index.php is my controller)

Code: Select all
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
Options All -Indexes

RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteRule ^Agencies/([^/]+)/([^/]+)/(.*)$ index.php?sector=2&pg=$2&qs=$3 [NC,L]
RewriteRule ^Agencies/([^/]+)/(.*)$ index.php?sector=2&pg=$2 [NC,L]
RewriteRule ^Agencies/(.*)$ index.php?sector=2&pg=$1 [NC,L]
RewriteRule ^Agencies$ index.php?sector=2 [NC,L]
RewriteRule ^Agency$ index.php?sector=2 [NC,L]

RewriteRule ^Providers/([^/]+)/([^/]+)/(.*)$ index.php?sector=4&pg=$2&qs=$3 [NC,L]
RewriteRule ^Providers/([^/]+)/(.*)$ index.php?sector=4&pg=$2 [NC,L]
RewriteRule ^Providers/(.*)$ index.php?sector=4&pg=$1 [NC,L]
RewriteRule ^Providers$ index.php?sector=4 [NC,L]
RewriteRule ^Provider$ index.php?sector=4 [NC,L]


ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php



I'd also like to protect one single file (securefile.php) with a password required to access it
is that easy to add ?
I've only proteced directories before..

thanks
oldgit
 
Posts: 3
Joined: Wed Aug 26, 2009 1:58 pm
Location: London

Postby richardk » Sat Aug 29, 2009 2:27 pm

I have this in my htaccess and most of the time it works fine but every now and then I seem to get a loop - any ideas what is wrong

For what URLs?

(also I'm not sure if I need the RewriteBase entry)

You only need it if it isn't the current directory (the directory the .htaccess file is in).

Try
Code: Select all
Options All
Options +FollowSymlinks -Indexes

ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php

RewriteEngine On

RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteRule ^Agencies/[^/]+/([^/]+)/(.*)$ index.php?sector=2&pg=$1&qs=$2 [NC,L]
RewriteRule ^Agencies/[^/]+/(.*)$         index.php?sector=2&pg=$1       [NC,L]
RewriteRule ^Agencies/(.*)$               index.php?sector=2&pg=$1       [NC,L]
RewriteRule ^Agenc(y|ies)$                index.php?sector=2             [NC,L]

RewriteRule ^Providers/[^/]+/([^/]+)/(.*)$ index.php?sector=4&pg=$1&qs=$2 [NC,L]
RewriteRule ^Providers/[^/]+/(.*)$         index.php?sector=4&pg=$1       [NC,L]
RewriteRule ^Providers/(.*)$               index.php?sector=4&pg=$1       [NC,L]
RewriteRule ^Providers?$                   index.php?sector=4             [NC,L]


I'd also like to protect one single file (securefile.php) with a password required to access it
is that easy to add ?
I've only proteced directories before..

Try adding
Code: Select all
<Files securefile.php>
  AuthType basic
  AuthName "private area"
  AuthBasicProvider file
  AuthUserFile /usr/local/apache/passwd/passwords
  Require valid-user
</Files>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby oldgit » Sun Aug 30, 2009 2:33 pm

Hi - thank you very much for your advice
you removed some brackets on this (and the corresponding line) but I found it didn't work so I put them back

Code: Select all
yours:
RewriteRule ^Agencies/[^/]+/(.*)$ index.php?sector=2&pg=$2 [NC,L]

mine:
RewriteRule ^Agencies/([^/]+)/(.*)$ index.php?sector=2&pg=$2 [NC,L]


I'm not really sure what they are doing but it seems they are needed

my loop is happening when I return back from my payment provider -
we land in a protected sub directory in which I have an htaccess file with
rewrite egnine off

(the sub directory is protected using Plesk directory contro, so not with htpasswd - payment gateway returns response with username/pass)

the script runs in the protected directory and at the end does a PHP redirect to go back to main root level site to the designated "show the result to the public" page
- at this point it goes into a loop as if it still thinks it should be going to the subdirectory

I have a PHP 'exit' after the redirect and the url is the full url to the page (including the https://etc ...)

also FYI: the "$" numbers are correct ($2 and $3 instead of $1 and $2) beacuse I am discarding the $1 item - that is a only user friendly name for the page - I identify the page I want using the id number in position 2

Maybe I need to run some more tests right now -
also now you've shown me how to protect one file maybe I can put the payment return file handler back in the root directory, which is where it was during development and where it worked fine!

thanks again for your help
oldgit
 
Posts: 3
Joined: Wed Aug 26, 2009 1:58 pm
Location: London

Postby oldgit » Mon Aug 31, 2009 1:04 am

I was thinking about my " RewriteBase / "

I've removed it now..

But I was wondering if that meant that if I was in a sub directory of root, then the modRewrite would come into force and override any local "Rewrite engine off" - and then go into a loop

do you think that might have been some of the problem?
oldgit
 
Posts: 3
Joined: Wed Aug 26, 2009 1:58 pm
Location: London

Postby richardk » Mon Aug 31, 2009 8:05 am

you removed some brackets on this (and the corresponding line) but I found it didn't work so I put them back

Code: Select all
yours:
RewriteRule ^Agencies/[^/]+/(.*)$ index.php?sector=2&pg=$2 [NC,L]

mine:
RewriteRule ^Agencies/([^/]+)/(.*)$ index.php?sector=2&pg=$2 [NC,L]


I'm not really sure what they are doing but it seems they are needed

Because that isn't my rule. My rule uses $1 not $2 which should work fine.

(the sub directory is protected using Plesk directory contro, so not with htpasswd - payment gateway returns response with username/pass)

There isn't anything about password protection (or "auth") in the sub directory .htaccess file?

Try adding a 401 (Unauthorized) ErrorDocument
Code: Select all
ErrorDocument 401 /some/file

to your document root .htaccess file.

the script runs in the protected directory and at the end does a PHP redirect to go back to main root level site to the designated "show the result to the public" page

Could you provide an example of this URL.

I have a PHP 'exit' after the redirect and the url is the full url to the page (including the https://etc ...)

And you are sure it does not contain the sub directory?

also FYI: the "$" numbers are correct ($2 and $3 instead of $1 and $2) beacuse I am discarding the $1 item - that is a only user friendly name for the page - I identify the page I want using the id number in position 2

Until you remove the unneeded () and then they need changing. The number referrers to the ()'s position.

I was thinking about my " RewriteBase / "

I've removed it now..

But I was wondering if that meant that if I was in a sub directory of root, then the modRewrite would come into force and override any local "Rewrite engine off" - and then go into a loop

do you think that might have been some of the problem?

No, i don't think that is part of the problem. The RewriteBase is applied to the new URL if it is relative, for example
Code: Select all
RewriteBase /abc/
/1 will go to /2
RewriteRule ^1$ /2 [QSA,L]
//3 will go to /abc/4
RewriteRule ^2$ 4 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 28 guests

cron