.htaccess and 301 redirect (dynamic pages) wordpress

点点圈 提交于 2019-12-25 02:00:57

问题


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!


回答1:


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]



回答2:


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!



来源:https://stackoverflow.com/questions/12964037/htaccess-and-301-redirect-dynamic-pages-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!