【问题标题】:.htaccess 301 Path URL Restructure?.htaccess 301 路径 URL 重组?
【发布时间】:2018-09-25 20:06:59
【问题描述】:

我阅读了很多关于完整域的 301 重定向的内容,但不知道如何准确地将现有路径更改为新路径。

这是一个例子:

site.com/cars site.com/motobikes

还有:

site.com/cars/tires sote.com/motobikes/(变量,例如柏林、巴黎)/tires

我怎样才能完成这项任务?

【问题讨论】:

    标签: php regex apache .htaccess


    【解决方案1】:

    试试这个:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule ^cars http://example.com/motorbikes [R=301,L]
    

    还有:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule ^cars/tires http://example.com/motobikes/berlin/tires [R=301,L]
    

    【讨论】:

    • 您好,谢谢您的回答。它实际上是这样工作的。我需要的是柏林作为一个城市成为一个变量。我应该添加 (.*)$
    • 您的意思是查询字符串吗?然后你可以添加 ?city=berlin 像:example.com/motobikes?city=berlin
    【解决方案2】:

    您可以在 PHP 中像这样进行重定向

    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.example.com/$var"); 
        exit();
    

    或者您可以使用 .htaccess 文件Redirect 301 /cars http://example.com/motobikes

    【讨论】:

    • 使用 php.ini 的好技巧。将尝试实施。在 /cars/($variable) 之后我需要一个变量。
    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多