Removing unwanted characters from URL in htaccess

2019-05-22 17:02发布

问题:

Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend

However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.

What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend

Thank you for your help.

回答1:

Add this to the top of your rules:

RewriteRule ^/?(.*)>$ /$1 [L,R=301]

You can use > because the URI gets decoded when matching in a RewriteRule.