简体   繁体   中英

.htaccess 301 Redirection Parameter to Wordpress?

I have migrated one of our sites from a PHP coded website to a WordPress website and I am having a problem with redirecting the old shop pages to our new shop pages.

I would like to do a 301 Redirect for this link:

http://www.example.com/products_promoDC.php?Product_Category=Corporate+Gifts

to:

http://www.example.com/product-category/corporate-gifts/

Everything I add does not work at all.

Currently I have:

# 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

# GZIP COMPRESSION
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# END GZIP COMPRESSION

Any help would be greatly appreciated!

EDIT

I have changed my .htaccess code:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^Product_Category=Corporate+Gifts$ [NC]
RewriteRule ^products_promoDC\.php http://www.example.com/product-category/corporate-gifts? [L,R=301]

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

# END WordPress

So I solved my own problem.

I redirected my products_promoDC.php to /product-category/ so the Variable/Parameter is then only loaded behind ( /product-category/Corporate+Clothing/ ) this still gave me a 404 error because of the plus ( + ) within the URL. I simply then wrote a RewriteRule to convert the pluses ( + ) to dashes!

RewriteRule "^([^+]*)\++([^+]*\+.*)$" /$1-$2 [N]
RewriteRule "^([^+]*)\++([^+]*)$" /$1-$2 [L,R=301]

RewriteCond %{QUERY_STRING} Product_Category=([^&]+) [NC]
RewriteCond %{QUERY_STRING} Product_Subcategory=([^&]+) [NC]
RewriteRule products_promoDC\.php product-category/%1/? [L,R=301]

I added a second line for my subcategories to load as well!

Working perfectly:

http://www.example.com/products_promoDC.php?Product_Category=Corporate+Gifts

What about

RedirectMatch 301 ^/products_promoDC\.php\?Product_Category\=Corporate\+Gifts/(.*)$ /product-category/corporate-gifts/$1

If you're new to htaccess, I recommend you to use htaccess generator

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