繁体   English   中英

.htaccess mod重写以将查询字符串重定向到路径

[英].htaccess mod rewrite to redirect query string to a path

我最近更新了网站,以使用:

www.website.com/path/123456

代替:

www.website.com/path/file.php?id=123456

为了不破坏旧版链接/书签,我想将旧查询字符串重定向到新路径。

我试过了:

RewriteRule ^path/file.php?id=?$ /path/$1 [L,R]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^path/.* /path/$1 [R]

RewriteCond %{REQUEST_URI} ^/path/$ [NC]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule .* /path/$1 [R]

RewriteCond %{REQUEST_URI} ^/path/file.php$ [NC]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule .* /path/$1 [R]

和一些变化,但没有运气。 我如何设置我的.htaccess来执行此重定向?

看起来问题在于您的正则表达式未捕获参数,如下所示:

RewriteRule ^path/file.php?id=(.*)$ /path/$1 [L,R]

在正则表达式中,请使用括号捕获要稍后引用的字符串部分,例如$1 .*将匹配任何内容。 因此,例如,如果您只想捕获数字,则可以将其更改为(\\d+)

您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /path/file\.php\?id=(\d+) [NC]
RewriteRule ^ /path/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^path/(\d+)/?$ path/file.php?id=$1 [L,QSA,NC]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM