【问题标题】:Can i use multiple RewriteRules for the same url .htaccess我可以为同一个 url .htaccess 使用多个 RewriteRules
【发布时间】:2021-11-22 16:26:09
【问题描述】:

我的 .htaccess

Options -Indexes
DirectorySlash Off
RewriteEngine On
RewriteRule ^about$ about/about_me.html [L]

这样可以吗

Options -Indexes
DirectorySlash Off
RewriteEngine On
RewriteRule ^about$ about/about_me.html [L]

#Using multiple RewriteRules with diffrent names for same url
RewriteRule ^About$ about/about_me.html [L]
RewriteRule ^ABOUT$ about/about_me.html [L]

【问题讨论】:

  • 你为什么要首先这样做? URL 不区分大小写。
  • @tacoshy “不区分大小写”?那为什么重写模块中实现了NC标志呢?
  • 是的,这是可能的。事实上,你做到了。宇宙并没有因此而内爆;-)
  • @arkascha 因为如果您想将 url 用于 url 缩短器、base64 或加密,您可以使 url 区分大小写。但是只有特定的部分才会区分大小写。默认情况下不是。所以除非他没有改变正常的不区分大小写的行为,否则没有区别。
  • 好的,谢谢你的回答我完全忘记了网址是区分大小写的,愚蠢的我:):)

标签: html css .htaccess web


【解决方案1】:
RewriteRule ^about$ about/about_me.html [L]

#Using multiple RewriteRules with diffrent names for same url
RewriteRule ^About$ about/about_me.html [L]
RewriteRule ^ABOUT$ about/about_me.html [L]

是的,你可以这样做,但是……你不应该这样做。

/about/ABOUT 严格来说是不同的 URL。 (URLs are case-sensitive.) Google 将这些视为不同的 URL,因此如果 /about/ABOUT 都提供相同的内容,那么您可能会创建重复内容问题(这是否真的会成为问题是另一回事)。

旁白:您不需要多个指令来执行此操作,而是为每个变体创建另一个指令。您可以在RewriteRule 指令上使用NC (nocase) 标志,使其成为不区分大小写的匹配。例如:

RewriteRule ^about$ about/about_me.html [NC,L]

这将匹配aboutABOUTAbOuT 等,并在所有情况下将请求重写为about/about_me.html

不区分大小写并避免重复内容 - 改为重定向

为了让about 的所有大小写变体均可访问并避免重复内容问题,您应该将非规范变体(例如ABOUTAbOuT 等)从外部重定向(而不是“重写”)到规范的about。并且只将about 重写为about/about_me.html

例如,上述“重写”之前,您可以实现以下“重定向”以将ABOUTAbOuT 规范化为about

# Redirect "ABOUT" and "AbOuT" to "about"
RewriteCond %{REQUEST_URI} ^/about$ [NC]
RewriteRule [A-Z] /about [R=301,L]

但是,此规则特定于一个 URL - 如果您有多个 URL,则不太实用。您可能会选择简单地将所有请求的 URL 小写。例如:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    相关资源
    最近更新 更多