views:

50

answers:

3

Hello, I have a problem with RewriteRules.

I'm currently using this rule:

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

This works fine for links as these, where $_GET['clip'] is the value after the slash:

http://example.com/abcdefg

But not for those with a dot character, like these:

http://example.com/abcdefg.png

How should I change the RewriteRule to make it work for such links?

A: 

You are matching on any string that does not contain "/" or ".". If you want the match to include the extension, you can use:

RewriteRule ^([^/]+)/?$

If you don't care about it:

RewriteRule ^([^/.]+)
nhnb
This together with jordanstephens answer makes perfect. Thanks!
Angelo Geels
+1  A: 

try this code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?clip=$1

This will prevent it from ignoring files that are really on your server. so http://example.com/abcdefg.png will work, as long as /abcdefg.png exists.

Also the regex you used in the rewrite rule needs to be altered slightly, I removed the . as you were preventing it.

jordanstephens
Read the comment I added to nhnb. Thanks! :)
Angelo Geels
+1  A: 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?clip=$1

i wouldn't suggest this technique because it will cause an extra disk read to check if the file is there... if you have the possibility i would advise to organize your directory structure in a way that apache can tell from the directory what to do. example from my framework:

  # skip whitelisted directories
  RewriteRule ^(test|skin|stylesheets|images|javascripts|favicon|robots\.txt|index\.php) - [L]
  RewriteRule ([-_\w\/]*) index.php?p=$1 [NE,NC,QSA]
Joe Hopfgartner