.htaccess - 需要 301 包含特殊字符的 URL
我有一个来自旧网站的长 URL,需要 301,例如:domain.com/web/vehicle/655520/2007-Hummer-H2---?sort_by=year&args=All_years--All_makes--All_models- -All_body_types--All_vehicles
我需要将其(以及更多类似的网址)重定向到重新设计的网站上的新页面,页面示例:domain.com/hummer.php
如何去除特殊字符(例如 ---? )以及 URL 中的其他所有内容以便我可以成功使用 301?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能用 mod_rewrite “剥离”任何东西。
您只能从字符串的一部分创建引用并使用它们来构建新的 url。
如何做到这一点取决于您想要根据原始 url 构建什么 url。
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.
为什么需要这样做?除非您计划将一长串重定向编码到 .htaccess 文件中,否则您应该在 PHP 中执行所有重定向。
从您提供的 URL 示例中,我假设所有项目都有一个已与 URL 绑定的唯一 ID。在这种情况下,您可以在数据库中创建一个地图,表明项目 655520 的“正确”URL 是 hummer.php。您可以使用它从 PHP 执行重定向。
以下是如何执行此操作的示例。我假设您已经有一个 .htaccess 文件,它将 URL 转换为 GET。类似于
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
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]