0

I have a link like this:

From: http://www.example.com/wp/?p=231

and I need to redirect it to this URL:

To: https://www.example.com/jump/

How could this be done?

This the .htaccess file:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,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

2 Answers 2

1

You need to use mod_rewrite to perform this specific redirect. And this rule should go at the very top of your .htaccess file, before all existing rules. For example:

RewriteEngine On

RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{QUERY_STRING} ^p=231$
RewriteRule ^wp/$ https://%{HTTP_HOST}/jump/ [QSD,R=302,L]

UPDATE: Added the two additional conditions to check the SERVER_PORT and HTTP_HOST so that it only redirects http://www.example.com/wp/?p=231 exactly as stated. It then redirects directly to https://www.example.com/jump/.

The additional condition that checks against the QUERY_STRING server variable is necessary in order to match the query string, since the RewriteRule pattern matches against the URL-path only.

The QSD flag is necessary to remove the existing query string from the redirected response, otherwise this is passed through by default.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for the answer but it didn't work for my .htaccess below: RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ example.com/$1 [R,L] RewriteEngine On RewriteCond %{QUERY_STRING} ^p=231$ RewriteRule ^wp/$ /jump/ [QSD,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
@Mete What do you mean "didn't work" exactly? Do you see a redirect? Incorrect redirect? Error? Nothing? This rule would need to go at the very top of the .htaccess file, before all your existing rules. What is the significance of the HTTP vs HTTPS. Do you specifically only want to redirect the HTTP version of this URL? Are you hosting multiple domains?
I just applied your update to the script but I don't see any redirect for some reason. So it returns the original http URL: example.com/wp/?p=231. No we are not hosting multilple domains. The link we like to redirect is broken and it is http for some reason and we would like to redirect to the existing https page(/jump/ )as in the post.
@Mete If you are not seeing a redirect then either 1. The .htaccess file is not being processed (seems unlikely given the WP directives that follow - although it is possible this is handled by the server config instead?) OR 2. This particular request is not reaching your server for some reason? If you check the HTTP response headers do you see anything unusual? What does the server header report? Are you using a CDN or front-end proxy of some kind? You mentioned (in other comments) that it returns a "blank page"? What is the HTTP status?
0

Depending on your .htaccess file you posted, the solution should be the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^/wp/ [NC]
    RewriteCond %{QUERY_STRING} p=231
    RewriteRule (.*) /jump? [R=301]

    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

You can check it with the following link:

https://htaccess.madewithlove.com?share=d5a7976b-47f3-4143-bb7c-103a722f0b2d

8 Comments

What about the query string?
It will be included at the end. You can check it here: htaccess.madewithlove.com
Thank you for your answer. For some reason, your answer didn't work for my .htaccess script. Please see my updated post with the original script.
@Mete I updated my answer depending and edited your .htaccess. You can check the links, there you see at the bottom that the new URL is example.com/jump when the pattern is met. :)
thank you again but it didn't work for me. My URL link is actually : example.com/wp/?p=231 which is not Https! if i test this with the testing site it returns as https "example.com/wp/?p=231". My site returns http as: "example.com/wp/?p=231" :-) All the other links on the websites are Https and return correctly except these conditional url links. :-(
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.