【问题标题】:301 redirect file to clean url301 重定向文件到清理 url
【发布时间】:2011-06-19 02:41:04
【问题描述】:

为我的页面处理重定向。我有一个所有旧网址和新网址的列表。

旧的 url 是 /directory/subdirectory/file.php

新的网址是干净的网址

/目录/子目录/文件/

一些新的 url 匹配没有“.php”的旧 url,而另一些则不匹配。

推荐一个网页:

RedirectMatch 301 ^/sales/products/item$ /products/item-name/

但以下内容会给我一个重定向循环吗?

RedirectMatch 301 ^/privacy-policy$ /privacy-policy/

我还没有尝试过,但希望不会给自己带来麻烦。

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:

    使用 Apache 的 mod_rewrite -- 将这些行添加到您的 .htaccess 文件中:

    # Activate Rewrite Engine
    RewriteEngine On
    
    # redirect *.php to "nice" url
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)\.php$ $1/ [R=301,QSA]
    
    # process "nice" urls by looking for appropriate .php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1.php [L,QSA]
    

    这些规则将做两件事:

    1) 将 (301) *.php 重定向到“漂亮”的网址:directory/subdirectory/file.php 将变为 directory/subdirectory/file/

    2) 正确地提供此类“不错”的网址,例如directory/subdirectory/file/ 将被视为directory/subdirectory/file.php

    P.S.如果重定向不起作用(有时可能会根据配置发生)尝试在 $1 之前添加斜杠(例如 /$1

    【讨论】:

    • 谢谢,但是对于具有不同 url 名称的页面呢?我在 /directory/subdirectory/file.php 有一些旧页面需要重定向到 /new-url-name/
    • 有几种方法可以做到这一点。最简单的(可能取决于细节)是专门为这些 URL 创建更多的重写规则。您能否举几个例子(从 => 到)以及此类重定向的大致数量。
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2016-05-11
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多