tags:

views:

1216

answers:

2

There doesn't seem to be much info on this topic so I'm going to outline my specific problem then maybe we can shape the question and the answer into something a bit more universal.

I have this rewrite rule

RewriteEngine On
RewriteBase /bookkeepers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?franchise=$1

Which is changes this URL

http://example.com/location/kings-lynn

Into this one

http://example.com/location/index.php?franchise=kings-lynn

The problem I am having is that if I add a trailing slash

http://example.com/location/kings-lynn/

then the query string is returned as

franchise=kings-lynn/

and for some reason none of my CSS and Javascript files are being loaded.

Any ideas?

+5  A: 

It looks like the (.+) is being greedy matched. In that case, you could try

RewriteRule ^(.+[^/])/?$ index.php?franchise=$1

This makes sure that the first group (in the brackets) doesn't end in a slash.

Paul Tomblin
Nice. This fixed my trailing slash problem (but not the lack of CSS issue).Don't suppose you could talk through how this works could you?
Binarytales
The regular expression matches a group consisting of 1 or more of any characters, followed by any character other than a slash. Outside the group you're optionally matching a slash. Before my change, the slash might have ended up in the group.
Paul Tomblin
+5  A: 

As @Paul Tomblin said, the .+ is being greedy; that is, it's matching as much as it can.

^(.+[^/])/?$ tells it to match anything, followed by a character that isn't a /, then followed by an optional /. This has the effect of not capturing the trailing /.

The most probable reason your CSS and Javascript doesn't work is you're using a relative path, like src="my.js". When there's a trailing slash, it looks like a directory, so your browser will look for /location/kings-lynn/my.js. You can fix this simply by using an absolute path to your files (e.g. /location/my.js).

Greg