htaccess RewriteRule problem

我的梦境 提交于 2019-12-24 10:37:22

问题


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?


回答1:


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]



回答2:


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.




回答3:


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 ^([^/.]+)


来源:https://stackoverflow.com/questions/3430693/htaccess-rewriterule-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!