重定向现有的文件,以不同的使用mod_rewrite的URL [英] Redirect existing file to different url using mod_rewrite

查看:145
本文介绍了重定向现有的文件,以不同的使用mod_rewrite的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用mod_rewrite现有文件重定向到一个不同的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]

一个偶然的机会是否有事可做的URL PARAMS?

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

推荐答案

您不能在重写规则的查询字符串。重写规则只匹配URL的一部分,这就是为什么你的新规则是行不通的。你需要有一个单独的RewriteCond匹配查询字符串。应该重新写成这样:

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]

这篇关于重定向现有的文件,以不同的使用mod_rewrite的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆