Mete
Mete

Reputation: 9

.htaccess URL redirect with parameters

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

Upvotes: 0

Views: 718

Answers (2)

Kevin Glier
Kevin Glier

Reputation: 1386

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

Upvotes: 0

MrWhite
MrWhite

Reputation: 46012

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.

Upvotes: 1

Related Questions