简体   繁体   中英

.htaccess and 301 redirect (dynamic pages) wordpress

I read all the other similar questions, but could not understand how to set it up so my old pages redirect where they should.

Here is my set up:

My old site pages:

http://www.oldsite.com/blog/?p=1234
http://www.oldsite.com/blog/?p=432
http://www.oldsite.com/blog/?p=xxxx

I would like to redirect the first two like so:

http://www.oldsite.com/blog/?p=1234 -> http://www.newsite.com/somewhere/on/mysite/
http://www.oldsite.com/blog/?p=432 -> http://www.newsite.com/somewhere/else/on/mysite/

and have all other pages (123, 321, 567, 999, ...) redirect to my home page like so:
http://www.oldsite.com/blog/?p= * **** -> http://www.newsite.com/

Thanks in advance!

You'll need to use mod_rewrite's ability to match against the query string, but all of the rules will have to be before any wordpress rules.

RewriteEngine On

# http://www.oldsite.com/blog/?p=1234 -> http://www.newsite.com/somewhere/on/mysite/
RewriteCond %{QUERY_STRING} ^p=1234$
RewriteRule ^/?blog/$ http://www.newsite.com/somewhere/on/mysite/ [L,R=301]

# http://www.oldsite.com/blog/?p=432 -> http://www.newsite.com/somewhere/else/on/mysite/
RewriteCond %{QUERY_STRING} ^p=432$
RewriteRule ^/?blog/$ http://www.newsite.com/somewhere/else/on/mysite/ [L,R=301]

# http://www.oldsite.com/blog/?p=***** -> http://www.newsite.com/
RewriteCond %{QUERY_STRING} ^p=[0-9]+$
RewriteRule ^/?blog/$ http://www.newsite.com/ [L,R=301]

I tried @Jon Lin code, but it did not work for my WordPress site. I think the problem was that I am running WordPress... I should have included my .htaccess code, but I was not thinking that WP would be the problem...

Here is what I had to do in order to make it work: Because it is WP site you need to put the code in the WP mod_rewrite. Here is my .htaccess before the redirects:

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

and after:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/blog/$
RewriteCond %{QUERY_STRING} ^p=998$
RewriteRule . /some/where/over-there? [L,R=301]

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

Hope this will help other people running WP and having the same problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM