修复 HTACCESS 文件中的重写规则和条件
好吧,假设我的 htaccess 文件中有以下代码,
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
我想知道如何使用上述代码将 url 中的某些扩展重定向到我的网站 404 页面。
例如,如果此链接 mywebsite.com/forums
末尾有任何扩展名,例如 .asp、.php、.html 等,那么它将被重定向到我的 404 页面。
顺便说一句,我如何将最后一个 RewriteRule 限制为仅某个正斜杠,其中 mywebsite.com/forums/2
将显示页面正常以及该特定限制之后的任何内容,例如 mywebsite.com/forums/2/so on...
将被重定向到我的 404 页面。
有人有什么想法吗?
Well lets say I have this follow code in my htaccess file,
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
I was wondering how would I, with the above code, redirect certain extensions in a url to my websites 404 page.
For instance, if this link mywebsite.com/forums
has any extension at the end of it such as .asp, .php, .html, and so forth it then would get redirected to my 404 page.
And on a quick side note how can I limit the last RewriteRule to only a certain forward slash where mywebsite.com/forums/2
would show the page fine and anything after that certain limit such as mywebsite.com/forums/2/so on...
would be redirected to my 404 page.
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解了这个问题,那么您需要巩固正则表达式以仅匹配您真正想要的模式 - 目前,它们对于您的需求来说有点太宽松了。
例如:
RewriteRule ^([^/]+)$ $1.php
这将匹配没有尾部斜杠的任何内容,而如果您想将其限制为仅匹配没有尾部斜杠的内容并且由字母数字字符组成,那么您可以这样做:
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
(您只能通过使用某些扩展来实现相同的效果前瞻断言,但这使您的正则表达式变得复杂,我觉得考虑您真正想要匹配的模式可能更明智(并且更容易头脑),然后预先表达这些模式。)
同样,您的后一个示例:
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
将匹配以字符串 forums/ 开头,后跟一个或多个数字,无论后面是否有任何内容。如上面那样添加结束锚点 ($)
RewriteRule ^forums/([0-9]+)$ ...
将断言字符串在数字之后结束。
这依赖于这样一个事实:如果 mod_rewrite 找不到匹配项,它不会尝试任何重写,并且(在该路径上没有任何显式资源的情况下)将落入 Apache 的 404 处理,然后由您来覆盖。
If I understand the question properly, then you need to firm up the regular expressions to only match the patterns you really want - at the moment, they're a bit too lenient for your needs.
For example:
RewriteRule ^([^/]+)$ $1.php
This will match anything without a trailing slash, whereas if you wanted to restrict it to only match, say, things without a trailing slash and consisting of alphanumeric characters, then you might do this:
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
(You could achieve the same effect for certain extensions only by using a lookahead assertion, but that complicates your regular expression. I feel it's probably saner (and easier on the mind) to think about the patterns you really want matched, and then express those up-front.)
Likewise, your latter example:
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
will match anything which starts with the string forums/, followed by one or more digits, whether or not there's anything after that. Adding an end anchor ($) as you have above
RewriteRule ^forums/([0-9]+)$ ...
will assert that the string ends after the digits.
This relies on the fact that if mod_rewrite can't find a match, it won't attempt any rewrites, and will (in the absence of any explicit resource at that path) fall through to Apache's 404 handling, which is then up to you to override.