5

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!

3 Answers 3

4

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.

4
  • Thanks so much for the great explanation of what was going wrong, Alexis!
    – emRae
    Sep 14, 2017 at 19:44
  • Sry - comment edit timed out on me. So I found out more – the way that url got to be /?post_type=email#038;p=14207 is it was pasted into an email and the #038; is the html code for the &. The problem is the /?post_type=email#038;p=14207 takes you to this view /email/ but stays the query url. If the & was not encoded this url query: /?post_type=email&p=14207 would redirect you to this post /email/fall-2017/ Neither using %23 and just & in the redirect worked. Any ideas of a better way to go about redirecting this url?
    – emRae
    Sep 14, 2017 at 19:54
  • 1
    "the #038; is the html code for the &" - Looks like something got corrupted, #038; isn't "the html code for &" - it's missing the & prefix. ie. it should be &#38; - the numeric HTML entity for &. Without the & prefix it would never be properly decoded.
    – MrWhite
    Sep 14, 2017 at 23:38
  • @emRae, ah, then you would get two parameters instead of one parameter and an anchor... Sep 15, 2017 at 1:59
1

Alexis Wilke really found out the issues. Along with the fact that the #038; in the url is the html code for &. It just got auto converted in the email and thus went to the wrong url.

The way I solved this was by using the WordPress plugin Quick Page/Post Redirect Plugin. The url parts that I did the redirect on was:/?post_type=email to /email/fall-2017/.

I could not do the regex or formatting correctly to get this to work in the .htaccess file and I think the plugin knows how to code that query string more accurately. Until I get better at regex, I'll stick with using the plugin for redirects.

1
  • As mentioned in my comment below, #038; is not actually a valid "html code" - it looks like it got corrupted, perhaps through some manual copy/pasting or something? All HTML entities start with a & and end with a ;.
    – MrWhite
    Sep 14, 2017 at 23:53
1

The way I solved this was by using the WordPress plugin Quick Page/Post Redirect Plugin. The url parts that I did the redirect on was: /?post_type=email to /email/fall-2017/.

I could not do the regex or formatting correctly to get this to work in the .htaccess file

The required code/regex is very similar (but even simpler) than what you posted initially, but just needs to go before the WordPress front-controller (as @AlexisWilke suggests).

For example:

# Special redirects
RewriteCond %{QUERY_STRING} ^post_type=email$
RewriteRule ^$ /email/fall-2017/? [R=302,L]

# 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

No need for the <IfModule> wrapper or additional RewriteEngine directive. And no need for the RewriteCond directive that checks against the REQUEST_URI server variable - this check is best done in the RewriteRule pattern ie. ^$ (empty URL-path).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.