开发者

.htaccess - need to 301 a URL with special characters

I have a long URL from an legacy website which I need开发者_StackOverflow中文版 to 301, example: domain.com/web/vehicle/655520/2007-Hummer-H2---?sort_by=year&args=All_years--All_makes--All_models--All_body_types--All_vehicles

I need to redirect this (and many more similar urls) to a new page on a redesigned website, page example: domain.com/hummer.php

How do you strip the special characters (ex. ---?) and everything else from the URL so that I can successfully use a 301?


You can't "strip" anything with mod_rewrite.
You can only create references from parts of a string and use them for building the new url.

How you can do it depends on what url you like to build out of the original url.


Why do you need to? Unless you're planning to code a long, long list of redirects into your .htaccess file, you should be doing all of your redirects in PHP.

From the URL example you gave, I assume all items have a unique ID that is tied to the URL already. In that case, you could create a map in your database that says that the "proper" URL for item 655520 is hummer.php. You can use that to perform a redirect from PHP.

Here's an example of how you can do this. I'm making the assumption that you already have an .htaccess file which translates the URL into a GET. Something like RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

//determine if you were passed a "legacy URL" (not shown)
if (legacyURL) {
  $urlComponents = $explode("/", $_GET['request']);
  $url = getItemUrl($components[2]);
  header("Location: " . $url,TRUE,301);
  exit();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜