使用 RewriteRule 仅将某些请求重定向到控制器文件

发布于 2025-01-03 01:51:24 字数 1033 浏览 2 评论 0原文

我创建了一个控制器文件,用于将用户重定向到正确的网址,我想将某些流量定向到该控制器而不影响其余流量。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2025-01-10 01:51:24

我仍然不知道你为什么试图把简单的事情变成复杂的;)

6 分钟查找+测试。这是应该起作用的:

RewriteCond %{QUERY_STRING} (^|(.)+&)(id=review-([0-9]+))($|&.*) [NC]
RewriteRule /shows.php /controller.php?%3 [QSA,L]

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:

RewriteCond %{QUERY_STRING} (^|(.)+&)(id=review-([0-9]+))($|&.*) [NC]
RewriteRule /shows.php /controller.php?%3 [QSA,L]

PS: Like Devin, I don't get/know why (?:^|&) works...

辞取 2025-01-10 01:51:24

有趣的。看起来不错,但我无法

RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]

在 mod_rewrite 中编译该行,直到我取出非捕获指示器

RewriteCond %{QUERY_STRING} (^|&)id=review-[0-9]+(&|$) [NC]

似乎在我的系统上编译正常,这对你有用吗?

Interesting. Doesn't' look wrong, but I couldn't get the line

RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]

to compile in mod_rewrite until I took out the non-capturing indicators

RewriteCond %{QUERY_STRING} (^|&)id=review-[0-9]+(&|$) [NC]

seems to compile ok on my system, does that work for you?

感情旳空白 2025-01-10 01:51:24

我认为您需要确定导致 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文