问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
