Michał Skrzypek
Michał Skrzypek

Reputation: 699

How to redirect a page that does not exist anymore in Wordpress?

When I first made my Wordpress site I didn't know it's a good idea to use slugs instead of ids in links. I had a page back then that had the address:

http://example.com/?page_id=50

When I made a template for a popular e-commerce platform in my country I included said link. A year later the site crashed and I had to rebuild it. Needless to say, the page in question was assigned with a different ID. I cannot change it on e-commerce platform (that would require thousands of individual changes - that is not an overstatement).

So, can I somehow redirect the /?page_id=50 to a different page? The problem is that no page has this particular ID.

Is there any way? I tried .htaccess redirection but it didn't work or maybe I did something wrong. I wrote

Redirect /?page_id=50 http://example.com/page-slug/

in .htaccess

I also tried (as suggested below):

# 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

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://example.com/page-slug/? [R=301,L]

as well as:

# 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]
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://lucart.pl/lucart-w-radio-eska/? [R=301,L]

</IfModule>

# END WordPress

Neither worked :(

Please help.

Upvotes: 1

Views: 908

Answers (2)

Abdoon Nur
Abdoon Nur

Reputation: 307

Please add this redirection php code at the very top of your theme header.php template before the html started:

$request_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    if($request_url=='http://example.com/?page_id=50'){
        wp_redirect('http://example.com/page-slug/ ');
    }

Upvotes: 3

Panama Jack
Panama Jack

Reputation: 24468

You can't use a query string with Redirect. You will need to use mod_rewrite and check for the query string.

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)page_id=50($|&)
RewriteRule . http://example.com/page-slug/? [R=301,L]

Upvotes: 1

Related Questions