大网数据广告活动
网站运营与故障问答

301 redirect in .htaccess - old URL includes ?page_id=

· 52CE 我爱测

I want to 301 redirect several URLs from an old WordPress installation within one subdirectory to a new WP installation within a different directory of the same domain. I.E.: http://www.example.de/abc/?page_id=123 needs to go to:…

问题原文

原帖链接 · Userin66193 · CC BY-SA 3.0

I want to 301 redirect several URLs from an old WordPress installation within one subdirectory to a new WP installation within a different directory of the same domain. I.E.:

http://www.example.de/abc/?page_id=123

needs to go to:

http://www.example.de/xyz/newindividualpage/

I have tried several options I found here and elsewhere, but so far I was unable to get it to work, resp. adapt found code correctly to my needs. Obviously the ?page_id= structure is where I am missing it.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

In order to match the query string (ie. page_id=123 part) you need a RewriteCond directive and the QUERY_STRING server variable (from mod_rewrite). Try something like the following in the /abc/.htaccess file (ie. in the subdirectory you want to redirect from):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=123
RewriteRule ^$ /xyz/newindividualpage/ [R=302,L]

(Change the 302 to 301 when you are sure it's working OK. 302 (temporary) redirects aren't cached by the browser, so makes testing easier.)

This redirects any URL within the /abc subdirectory that has a query string that starts page_id=123 (as per your example).


Alternatively, if you prefer to only use the .htaccess file in the document root, then add the following to the top of the .htaccess file (at least before any other mod_rewrite directives) in the root directory:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=123
RewriteRule ^abc/?$ /xyz/newindividualpage/ [R=301,L]

The RewriteEngine directive only needs to appear once in the file. It can appear anywhere in the file, although it is more logical if it appears at the top. (It doesn't matter if it occurs more than once, it's just unnecessary.)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。