skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search