简体   繁体   English

.htaccess-需要301具有特殊字符的URL

[英].htaccess - need to 301 a URL with special characters

I have a long URL from an legacy website which I need 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 我有一个来自需要访问301的旧网站的长URL,例如:domain.com/web/vehicle/655520/2007-Hummer-H2---?sort_by=year&args=All_years--All_makes--All_models--All_body_types -所有车辆

I need to redirect this (and many more similar urls) to a new page on a redesigned website, page example: domain.com/hummer.php 我需要将此(以及许多其他类似的URL)重定向到重新设计的网站上的新页面,页面示例: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? 您如何从网址中删除特殊字符(例如---?)和其他所有内容,以便我可以成功使用301?

You can't "strip" anything with mod_rewrite. 您不能使用mod_rewrite来“剥离”任何东西。
You can only create references from parts of a string and use them for building the new url. 您只能从字符串的一部分创建引用,并将其用于构建新的URL。

How you can do it depends on what url you like to build out of the original url. 如何做到这一点取决于您希望从原始URL中构建哪个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. 除非您打算将一长串的重定向列表编码到您的.htaccess文件中,否则您应该在PHP中进行所有重定向。

From the URL example you gave, I assume all items have a unique ID that is tied to the URL already. 从您提供的URL示例中,我假设所有项目都有一个已经绑定到URL的唯一ID。 In that case, you could create a map in your database that says that the "proper" URL for item 655520 is hummer.php. 在这种情况下,您可以在数据库中创建一个映射,指出条目655520的“正确” URL为hummer.php。 You can use that to perform a redirect from PHP. 您可以使用它来执行从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. 我假设您已经有一个.htaccess文件,它将URL转换为GET。 Something like RewriteRule ^(.*)$ index.php?request=$1 [L,QSA] 类似于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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM