【发布时间】:2019-04-01 04:09:25
【问题描述】:
有人可以帮我吗?我正在使用这个简单的语句将根 index.htm 重定向到根目录 /
Redirect 301 /index.htm /
但这会导致无限循环。它重定向到我的根目录,所以它完成了这项工作。但是现在不可能向根目录发送请求而不会收到错误消息告诉您有关无限重定向的信息。我的问题:为什么?以及如何避免这种情况?
€dit:现在我有了这个:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [NC,QSA,L]
RewriteRule ^((.*)/)?index.[(php)(htm)(html)] $1
[R=301,L]
如果我注释掉其中一个规则,另一个会完美运行。但是,在每种情况下,拥有这两个规则都会导致对根的重写。请帮忙!
€dit 2:
我想要的是用一个 index.php 文件处理所有传入请求,并将每个 index.php、index.htm 和 index.html 重定向到它的目录,因为内容重复。该怎么做?
€dit 3:
现在一切都像魅力一样!
RewriteEngine On
RewriteBase /
#Redirects http://host.tld/gamedev to http://gamedev.host.tld/
Redirect 301 /gamedev/ http://gamedev.host.tld/
#Redirects every request to a subdirectory's index file to the subdirectory because of duplicated content
RedirectMatch ^/(.*)/index.(php|html?)$ http://gamedev.host.tld/$1
#Remove index file from a root folder request
RewriteCond %{THE_REQUEST} \s/index\.(php|html?)
RewriteRule ^index\.(php|html?)$ / [R=302,L]
#Handling all incoming requests with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
非常感谢 达斯·穆恩
【问题讨论】:
-
您的 http 服务器很可能配置为将
index.htm文件作为目录索引提供。 -
但是没有我的规则 index.htm 不会被重定向到根目录。否则我不需要重定向
-
你能解释一下你想用 2 条重写规则(尤其是第 1 条)归档什么吗?也许一些例子会有所帮助。
-
第一个应该让 index.php 文件处理每个请求
-
@DarthMoon:我认为你应该看看 DocRoot 的答案。它应该完全符合您的要求。
标签: apache .htaccess redirect url-redirection http-redirect