且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htaccess的301重定向动态URL

更新时间:2022-10-21 16:59:32

的的 重写规则 并只在的 URL路径而不是网址查询。你需要使用的RewriteCond 指令来测试网址查询(%{QUERY_STRING} )。因此,尝试这条规则:

 的RewriteCond%{QUERY_STRING} ^(([^&放大器;] *放大器;)*)ID =([^&功放;] +)及?(。*)?$
重写规则^ oldpage \ .PHP $ http://new.example.com/newpage-%3?%1%4 [L,R = 301]
 

这条规则也将preserve其他参数的查询。

I am moving my site to new domain. Need to redirect pages
from
old-site.com/oldpage.php?id=X
to
new-site.com/newpage-X
(X is number)

Why this rule does not work?

RewriteEngine on
RewriteRule ^oldpage.php?id=(.*)$ http://new-site.com/newpage-$1 [R=301,L]

The RewriteRule does only operate on the URL path and not the URL query. You need to use the RewriteCond directive to test the URL query (%{QUERY_STRING}). So try this rule:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^oldpage\.php$ http://new.example.com/newpage-%3?%1%4 [L,R=301]

This rule will also preserve other parameters in the query.