Dynamic 301 Redirect

2019-08-02 20:59发布

问题:

We made a mistake in the scope of our dynamic pages on a new site, and have some incorrect pages already spidered in Google.

I need to redirect the following format: http://www.domain.com/dir/dir/?q=120

To this format: http://www.domain.com/dir/dir/?p=120

Only difference is the 'q' needs to be a 'p'.

RewriteEngine is on, as I've already consolidated traffic from domain.com to www.domain.com

This is what I have in my root .htaccess file:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^centerline.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
RewriteRule ^(/leadership/detail/)\?q=([0-9]+)$    $1?p=$2 [R=301, L]

回答1:

Try this:

RewriteCond %{QUERY_STRING} q=([0-9]+)$
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]

You'll need the RewriteCond as apache doesn't allow matching against the querystring in a RewriteRule

EDIT

RewriteCond %{REQUEST_URI} ^/leadership/detail/$
RewriteCond %{QUERY_STRING} q=([0-9]+)$ 
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]