【问题标题】:htaccess rewrite after/with redirecthtaccess 在重定向之后/使用重定向重写
【发布时间】:2016-02-04 19:58:59
【问题描述】:

当出现 404 缺失页面错误时,调用此 URL:

https://domain.com/404/

在 404 文件夹内只有一个 htaccess 文件将重定向到特定语言的错误页面(子文件夹中只有一个 index.html)。

/
  404
  .htaccess
     pt
      index.html
     en
      index.html

htaccess 文件代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# If Accept-Language starts with 'pt',
# then redirect (only) subdirectory 'pt'
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/?$ %{REQUEST_URI}pt [L,NC]

# Otherwise, redirect to en
RewriteRule ^/?$ %{REQUEST_URI}en [L,NC]
</IfModule>

葡萄牙语的最终 URL 如下所示:domain.com/404/pt/

我需要进行哪些更改才能使 URL 仍重定向到特定语言子文件夹中的 index.html,但看起来像 domain.com/404(带有或不带有斜杠)?

谢谢。

【问题讨论】:

  • 从外部重定向 404 是一个坏主意,因为这样依赖于 HTTP 状态代码信息的客户端(例如搜索引擎机器人)将无法获得他们最初请求的资源的状态代码不再是 404。

标签: .htaccess redirect mod-rewrite


【解决方案1】:

对于特定语言 404 的静默重写,请在 /404/ 子目录中使用此 .htaccess:

ErrorDocument 404 default
DirectoryIndex index.html
RewriteEngine On
RewriteBase /404/

# If Accept-Language starts with 'pt',
# then redirect (only) subdirectory 'pt'
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/?$ pt/index.html [L]

# Otherwise, redirect to en
RewriteRule ^/?$ en/index.html [L]

【讨论】:

  • 还不工作:浏览器 URL 读取 domain.com/404/ 但报告永无止境的重定向循环错误,并且未加载子文件夹(对应于语言)中的 index.html。
  • 没有 Chrome,但有 Firefox。它显示了许多 302,全部为 https://domain.com/404/
  • 在 404 htaccess 中只有其他语言规则,如 pt 之一。但我发现:当我在浏览器中输入domain.com/404/ 时,我收到此错误/var/www/virtual/username/domain.com/404/pt/ not found on this server。当我输入domain.com/404/pt/ 时,它会显示。
  • 是的,没关系,我认为您需要 RewriteBase 行以避免附加完整路径
  • 太棒了。现在它起作用了。我只需要调整 index.html 中的链接就可以了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
相关资源
最近更新 更多