使用 mod_rewrite 将现有文件重定向到不同的 url

发布于 2025-01-02 23:00:35 字数 338 浏览 3 评论 0原文

我正在尝试使用 mod_rewrite 将现有文件重定向到不同的 URL。我正在使用以下内容,但没有效果。我已经尝试了几种不起作用的变体。

RewriteEngine on
AddHandler x-httpd-php .php3
# AddHandler x-httpd-php5 .php .php4

# This file exists, but this redirect doesn't work
RewriteRule ^show.php?id=review-1$ /review/1/super-baseball-2020/ [R=301,L]

它是否偶然与 url 参数有关?

I'm trying to use mod_rewrite to redirect an existing file to a different URL. I'm using the following and it has no effect. I've tried several variations of which don't work.

RewriteEngine on
AddHandler x-httpd-php .php3
# AddHandler x-httpd-php5 .php .php4

# This file exists, but this redirect doesn't work
RewriteRule ^show.php?id=review-1$ /review/1/super-baseball-2020/ [R=301,L]

Does it by chance have something to do with the url params?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挽清梦 2025-01-09 23:00:35

RewriteRule 中不能有查询字符串。 RewriteRule 仅匹配 URL 部分,这就是您的新规则不起作用的原因。您需要有一个单独的 RewriteCond 来匹配 QUERY 字符串。它应该像这样重写:

# for external redirect from /shows.php?id=review-1 to /review/1/super-baseball-2020/
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=([^-]*)-(.*)(?:&|$) [NC]
RewriteRule ^ /%1/%2/super-baseball-2020/? [R=301,L,NC]

# for internal redirect from /review/1/super-baseball-2020/ to /shows.php?id=review-1
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ shows.php?id=$1-$2 [L,NC,QSA]

You cannot have query string in RewriteRule. RewriteRule matches only URL part that's why your new rule is not working. You need to have a separate RewriteCond to match QUERY string. It should be re-written like this:

# for external redirect from /shows.php?id=review-1 to /review/1/super-baseball-2020/
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=([^-]*)-(.*)(?:&|$) [NC]
RewriteRule ^ /%1/%2/super-baseball-2020/? [R=301,L,NC]

# for internal redirect from /review/1/super-baseball-2020/ to /shows.php?id=review-1
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ shows.php?id=$1-$2 [L,NC,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文