【问题标题】:.htaccess redirect multiple URLs to one .html.htaccess 将多个 URL 重定向到一个 .html
【发布时间】:2013-11-06 10:57:10
【问题描述】:

我正在尝试使用这样的 URL 结构:
www.mydomain.com/aaa/bbb/ccc
www.mydomain.com/aaa/ddd/eee
www.mydomain.com/aaa/fff/ggg

以上所有网址都应重定向到:www.mydomain.com/aaa.html

今天我在 .htaccess 中使用它:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]

如果我尝试在浏览器中输入:www.mydomain.com/aaa/bbb/ccc,服务器将查找:www.mydomain.com/aaa.html/bbb/ccc 而不是 www.mydomain.com/ aaa.html

【问题讨论】:

  • RewriteRule ^/?aaa(.*)/?$ aaa.html [L]
  • 看起来这应该可行,但发生了一些奇怪的事情。如果文件 aaa.html 不存在,那么我的浏览器会告诉我:“在此服务器上找不到请求的 URL /aaa.html”。但是如果文件 aaa.html 存在,那么我会得到:“在此服务器上找不到请求的 URL /aaa.html/bbb/ccc。”
  • 好的。这行得通。只要我不重定向到 aaa.html。然后我猜其他 RewriteCond “interreferes”。但我可以重定向到 aaa.shtml 或 aaa.php

标签: .htaccess


【解决方案1】:

我有类似的情况,我喜欢将所有请求从永久移动的目录重定向到新位置(在我的情况下为新域)

RewriteRule ^aaa\/?(.*)$ "http\:\/\/mydomain\.com\/aaa.html" [R=301,L]

注意 301,“永久移动”代码。该接缝要求在重定向路径中使用完整的 url。

希望对你有帮助。

【讨论】:

  • 感谢您的回答。我真的不需要 301,我希望这对用户是透明的。我认为该解决方案应该有效,但这是在一个子文件夹上,并且父文件夹也有一个 .htaccess 似乎覆盖了我正在更改的那个。我仍然得到“www.mydomain.com/aaa.html/bbb not found”,我无法删除父文件夹中的 .htaccess,还有什么可以阻止它应用到我的子文件夹吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2022-01-13
  • 2012-12-04
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多