使用 RewriteRule 仅将某些请求重定向到控制器文件
我创建了一个控制器文件,用于将用户重定向到正确的网址,我想将某些流量定向到该控制器而不影响其余流量。 http://website.com/shows.php?id=review-1
是我要发送到控制器的 url 示例。
到目前为止,我炮制的是以下 RewriteRule
,它会导致站点范围的服务器 500 服务器错误。
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
RewriteRule ^ /controller.php [QSA,L]
从 %{QUERY_STRING}
条件中删除 -[0-9]+
可以消除站点范围的 500 服务器错误,但不起作用。我尝试替换的以下 RewriteRule
效果很好。
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [QSA,L,NC]
最终结果是访问 http://www.website.com/shows.php?id=review-1
的用户最终将访问 http://www.website .com/controller.php?id=review-1
将处理 301 重定向。
I've created a controller file which handles redirecting users to the proper url, I want to direct certain traffic to this controller and not affect the rest. http://website.com/shows.php?id=review-1
is an example of the url I want to send to the controller.
What I've concocted thus far is the following RewriteRule
which causes a site-wide server 500 Server Error.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
RewriteRule ^ /controller.php [QSA,L]
Removing the -[0-9]+
from the %{QUERY_STRING}
condition gets rid of the site-wide 500 Server Error, but doesn't work. The following RewriteRule
that I'm trying to replace works just fine.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [QSA,L,NC]
The end result is that a user who goes to http://www.website.com/shows.php?id=review-1
will end up at http://www.website.com/controller.php?id=review-1
which will deal with handling the 301 redirect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我仍然不知道你为什么试图把简单的事情变成复杂的;)
6 分钟查找+测试。这是应该起作用的:
PS:像德文一样,我不明白/不知道为什么
(?:^|&)
起作用......I still don't know why you're trying to make simple things into complex ones ;)
6 minutes to find+test. Here's what should work:
PS: Like Devin, I don't get/know why
(?:^|&)
works...有趣的。看起来不错,但我无法
在 mod_rewrite 中编译该行,直到我取出非捕获指示器
似乎在我的系统上编译正常,这对你有用吗?
Interesting. Doesn't' look wrong, but I couldn't get the line
to compile in mod_rewrite until I took out the non-capturing indicators
seems to compile ok on my system, does that work for you?
我认为您需要确定导致 500 错误的原因。我怀疑重定向正常,但您重定向到的页面失败。
I think you need to ascertain what's causing the 500 error. I suspect the redirect is working, but the page you're being redirected to is failing.