htaccess RewriteRule problem
I have a problem with RewriteRules.
I'm currently using this rule:
RewriteRule ^([^/.]+)/?$ index.php?clip=$1
开发者_C百科
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?
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]
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.
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 ^([^/.]+)
精彩评论