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

Temporarily redirect WordPress url with query string from root directory to new url

· 52CE 我爱测

This post seems to have my answer: How can I redirect old WordPress URL (with a query string) to new URL using .htaccess? However, this does not seem to play nicely with WordPress' previous rewrite rules. What I would like to do …

问题原文

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

This post seems to have my answer:
How can I redirect old WordPress URL (with a query string) to new URL using .htaccess?

However, this does not seem to play nicely with WordPress' previous rewrite rules. What I would like to do is a temporary redirect (302) from this page: https://example.com/?post_type=email#038;p=14207 to this page: https://example.com/email/fall-2017/

Here is the relevant mod_rewrite rules in my .htaccess file and their order:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# BEGIN Query String URL redirects
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/$
    RewriteCond %{QUERY_STRING} ^post_type=email#038;p=14207$
    RewriteRule .* https://example.com/email/fall-2017/? [R=302,L]
</IfModule>
# END Query String URL redirects

A second pair of eyes is very appreciated!

社区高分解答

原帖链接 · Alexis Wilke · CC BY-SA 3.0

I think you have two problems.

One, the rewrite are checked in order and you put the more constraining one after the least constraining one (i.e. the redirect with your query string should be first.)

Second, we have another problem concerning the query string. It includes an anchor. That is never sent to the server (anything after #... is client side only.)

So the rewrite cannot take that hash in account, it won't make it to the server and therefore cannot be checked. However, it looks like it has important information, so I don't think you can do it right unless you know how it gets converted before it gets sent to the server. One possibility would be that it is written as %23.

Now, for you to have a good idea you'd have to look into the logs and see what you get there when that page gets hit.

Here I have an example what what you may see.

smsfromme.com:443 173.14.79.185 - - [14/Sep/2017:01:38:36 +0000] "GET /api/1/message/next HTTP/1.1" 404 141 "smsfromme.com" "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G386T Build/KOT49H)" TLSv1 ECDHE-RSA-AES128-SHA

The query string appears after the path, so you should see a GET followed by something like:

"GET /?post_type=email#038;p=14207" ...

Or as I suspect give the information you gave us, this:

"GET /?post_type=email" ...

And if your redirect works, the code after the path will be 302.

It is very likely that you have a JavaScript that takes the anchor (data after the #) and converts that in a GET for the data of the page (i.e. usually called an AJAX request.) It's a cool trick to avoid reloading the entire page each time. But it makes it harder to "tweak" with an .htaccess file.

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

资料来源与适用范围

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

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

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