Forum Moderators: phranque

Message Too Old, No Replies

Multiple URL Rewrite problem

         

cezuk

7:37 pm on Sep 13, 2007 (gmt 0)

10+ Year Member



I've spent far too long trying to get my head round this problem - I'm relatively new to Apache servers and so equally new to the world of mod_rewrite. I can't lose any more sleep over this so any help would be much appreciated!

The site I'm working on uses a scheme whereby http://example.com/a/b/...(etc) becomes http://example.com/index.php/a/b/...(etc)

On top of this, allowing access to specific subdirectories, as such:


RewriteCond %{REQUEST_URI} !/phpmyadmin/ [NC]
RewriteRule ^(([^/]+/)*[^/]+)/$ /index.php/$1 [L]

The problem I have is with trailing slashes. As it stands, http://example.com/a/b/ works, but http://example.com/a/b doesn't.

My initial thought was to put another condition above this, checking for the trailing slash then redirecting if one isnt found. I assumed that something like this should work:


RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(([^/]+/)*[^/]+)$ $1/ [R=302,L]
RewriteCond %{REQUEST_URI} !/phpmyadmin/ [NC]
RewriteRule ^(([^/]+/)*[^/]+)/$ /index.php/$1 [L]

But courtesy of Fiddler, I discovered I was sending myself into all sorts of infinite loops. Trying other methods sourced from Google brought similar results.

I don't know if maybe theres a way of writing it into the original Rewrite Rule to append the trailing slash as its adding the index.php?

A bit of info that may help:
- At present, the site won't go more than 4 layers deep. ie. http://example.com/a/b/c/d/ This may change, but I'll cross that bridge when I come to it ;)
- No urls should end in an extension, everything should appear as a directory in the url.

As I said, any help would be much appreciated. And I'll sleep better tonight. ;)

[edited by: jdMorgan at 10:55 pm (utc) on Sep. 13, 2007]
[edit reason] No URLs, please. [/edit]

sc112

8:39 pm on Sep 13, 2007 (gmt 0)

10+ Year Member



I will give it a go. The gurus on this forum will correct me if I am wrong. Your patterns seem needlessly complicated. How about:

RewriteCond %{REQUEST_URI}!/$
RewriteCond %{REQUEST_URI}!/phpmyadmin/ [NC]
RewriteRule ^(.*)/?$ /index\.php/$1 [L]

jdMorgan

11:52 pm on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some of the complexity noted above is due to the fact that the prototype of these rules support any number of leading directory levels. Let's add some notes and a RewriteCond and see if that doesn't make things clearer and fix the problem:

# Externally redirect to add a trailing slash
# if missing except for for URLs starting with
# /phpmyadmin or containing index.php/
RewriteCond %{REQUEST_URI} !/$
RewriteCond $1 !^phpmyadmin/ [NC]
RewriteCond $1 !index\.php/
RewriteRule (.*) http://www.example.com/$1/ [R=[b]301[/b],L]
#
# Rewrite all URLs except for URLs starting with /phpmyadmin
# or containing index.php/ to index.php/path
RewriteCond $1 !^phpmyadmin/ [NC]
RewriteCond $1 !index\.php/
RewriteRule ^(([^/]+/)*[^/]+)/$ /index.php/$1 [L]

You should carefully review this approach, because it is potentially problematic; Be sure you really want to rewrite all types of URLs, such as robots.txt, shared stylesheets and JavaScripts, etc. If so, you may wish to exclude them from being rewritten by URL-path, filetype, or directory location.

Jim