【问题标题】:.htaccess & 301: Redirect URL with capital letters to same URL but in lowercase.htaccess & 301:将带有大写字母的 URL 重定向到相同的 URL,但为小写
【发布时间】:2011-09-24 23:37:53
【问题描述】:

1) 我得到了 mysite.com/about-me.php。

当我输入 mysite.com/About-me.php 时,我收到 404 错误。

我想为我的 .htaccess 添加一些魔法,以便 mysite.com/My-PaGe.php (etc) redirects the user by 301, OR rewrites the URL (whatever is the best?) to mysite.com/my-page.php.
- 是301还是重写最好?

2) 我是否可以添加一条适用于所有 URL 的规则,还是我必须手动指定所有不同的页面?

奖励 3) 如果有人知道如何将 mysite.com/mY-paGe.php 重写为 mysite.com/my-site,则额外奖励星星

非常感谢

【问题讨论】:

    标签: .htaccess url-rewriting http-status-code-301 friendly-url


    【解决方案1】:

    这会进行 301 重定向,所有带有大写字母的 url 都会被重写为小写版本:

    RewriteMap lc int:tolower    
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteRule (.*) ${lc:$1} [R=301,L]
    

    另请参阅这篇文章,尤其是 cmets 部分。 http://www.chrisabernethy.com/force-lower-case-urls-with-mod_rewrite/

    可能需要在 httpd.conf 中指定这一行,并在 .htaccess 中指定 RewriteCond / RewriteRule

    RewriteMap lc int:tolower
    

    【讨论】:

    • @duncan ..and??如果您没有 RewriteMap 声明,它应该如何工作?这条规则本身没有任何用处。
    • 好点,我忘了地图。更新了我的答案以包含它。
    • @duncan 好吧..问题仍然“有点”——RewriteMap 不能在 .htaccess 中声明。至少记下这个不幸的时刻(如果它像那样工作会容易得多)。
    • @LazyOne,我有一个 RewriteMap 在我的声明中完全一样,我不确定我知道你的意思。另请参阅chrisabernethy.com/force-lower-case-urls-with-mod_rewrite 之类的示例
    • 好的,我看到有人说 RewriteMap 必须在单独的 httpd.conf 文件中完成。我不需要这样做,但我更新了我的答案以提及它作为一种可能性。
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 2013-12-21
    • 2015-03-04
    • 2013-10-28
    • 2016-05-23
    • 2016-09-28
    • 2023-03-25
    • 2013-01-27
    相关资源
    最近更新 更多