开发者

htaccess RewriteRule: filter certain words

I have

RewriteRule ^post/([^/]+)/([^/]+)$ /post/index.php?$1=$2 [NC]

which does what it's supposed to do: take a URL like post/colo开发者_开发知识库r/black and turn that into post/index.php?color=black.

The problem is that this also affects things like the stylesheet (located at post/styles/style.css), and other files that really exist.

So the question is: if I know the exact $_GET keys that need to be translated, how can I limit the above RewriteRule to only do its magic for those specific keys, but leave everything else untouched?

Thanks.


You can use:

^post/([^/]+)/(black|white|...)$

or

^post/([^/]+)/((?!bad keywords)[^/]+)$


You can either:

  • Use the -f flag to exclude actually existing resources from the rewrite process by adding the following RewriteRules:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
  • Or store your static resources outside /post - this would be best because you can exclude the possibility of collisions with 100% certainty, and you save Apache from looking the file up (which is only relevant with a lot of traffic though).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜