开发者

Regexp / htaccess rewrite fun

开发者 https://www.devze.com 2023-03-29 00:56 出处:网络
I have a long list of URLs from an old site structure that I need to redirect using RedirectMatch to new URLs on the same domain. The trick is that the old (source) URLs contain a bunch of messy varia

I have a long list of URLs from an old site structure that I need to redirect using RedirectMatch to new URLs on the same domain. The trick is that the old (source) URLs contain a bunch of messy variables. That's not a problem, right? I just put in place some sweet little Regexp statements to handle those variables. That's what I did开发者_Python百科, and it matches the variables correctly. Sweet!

The problem comes with the second part of the RedirectMatch statement - the destination. RedirectMatch is correctly resolving the old URLs to the new URLs, except that the old variables are appended to the new URL. I want to keep the redirects, but have the destination URL not contain the variables. Here is my code:

RedirectMatch 301 ^/Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html

Actual Redirect URL:

http://www.website.com/garage.html?launch_pg=itemZoomView&launch_sel=1009152&launch_pg_sp=true&title=Pig+Waste+Can

Can anybody point me to what I am doing wrong here? I just want to get rid of those crummy old variables and start fresh.


If by "variables" you mean query string (the launch_pg=itemZoomView&launch_sel=1009152&launch_pg_sp=true&title=Pig+Waste+Can part of your destination URL example), then use this redirect rule:

RedirectMatch 301 ^/Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html?

The only difference -- the ? at the end of new URL. This stops old query string from being copied over as we telling Apache that new URL will have this empty query string.


If you can use mod_rewrite, here is the rule:

# Activate Rewrite Engine
Options +FollowSymLinks
RewriteEngine On

# the rewrite rule
RewriteRule ^Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html? [R=301,L]

This definitely will redirect with empty query string.

0

精彩评论

暂无评论...
验证码 换一张
取 消