如何重定向“/”到“/home.html”仅当文件“/index.html”存在时物理上不存在吗?
我找到了一种将“/
”重定向(不是加载,而是更改 URL)到“/home.html
”的方法。现在我想添加一个 RewriteCond
以避免文件“/index.html
”存在时的重定向。
我尝试过(没有评论),但没有成功:
# We check that we comes from "domain.tld/"
RewriteCond %{REQUEST_URI} =/
# We check that there is no index.html file at the site's root
RewriteCond %{REQUEST_URI}index\.html !-f
# We redirect to home.html
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
帮助我,欧比旺·克诺比……你是我唯一的希望!
@Gumbo
它比上面的例子稍微复杂一些。事实上,我使用相同的 .htaccess 管理本地主机和生产开发,因此我尝试了类似的操作(按照您的答案):
# Redirect domain.tld/ to domain.tld/home.html (only if domain.tld/index.html does not exists)
RewriteCond %{DOCUMENT_ROOT}index\.html !-f [OR]
RewriteCond %{DOCUMENT_ROOT}domain.tld/www/index\.html !-f
RewriteRule ^$ %{REQUEST_URI}home\.html [R=301,L]
我查看了“%{DOCUMENT_ROOT}domain.tld/www/index.html”返回的路径这正是我的index.html 文件的路径...尽管如此,它也不起作用。 :(
顺便说一句,感谢“^$”避免“%{REQUEST_URI} =/”!\o/
知道为什么吗?
I found a way to redirect (not load, but change the URL) "/
" to "/home.html
". And now I want to add a RewriteCond
to avoid the redirection if the file "/index.html
" exists.
I tried (without the comments), but it didn't worked :
# We check that we comes from "domain.tld/"
RewriteCond %{REQUEST_URI} =/
# We check that there is no index.html file at the site's root
RewriteCond %{REQUEST_URI}index\.html !-f
# We redirect to home.html
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
Help me Obi-wan Kenobi... You're my only hope!
@Gumbo
It's a little bit more complicated than the above example. In fact, I manage both localhost and production development with the same .htaccess, so I tried something like this (following your answer) :
# Redirect domain.tld/ to domain.tld/home.html (only if domain.tld/index.html does not exists)
RewriteCond %{DOCUMENT_ROOT}index\.html !-f [OR]
RewriteCond %{DOCUMENT_ROOT}domain.tld/www/index\.html !-f
RewriteRule ^$ %{REQUEST_URI}home\.html [R=301,L]
I looked at the path returned by "%{DOCUMENT_ROOT}domain.tld/www/index.html" and it's exactly the path of my index.html file... nevertheless, it didn't worked too. :(
By the way, thanks for the "^$" astuce to avoid "%{REQUEST_URI} =/" ! \o/
Any idea why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文件检查
-f
需要有效的文件系统路径。但%{REQUEST_URI}index\.html
不是文件系统路径,而是 URI 路径。您可以使用-F
来通过子请求检查是否存在。或者使用DOCUMENT_ROOT来构建有效的文件系统路径:此外,其他条件可以通过
RewriteRule
模式来完成。当您在 .htaccess 文件中使用 mod_rewrite 时,相应的路径前缀将被删除(在文档根目录的情况下:/
),以便剩余的路径是一个空字符串(与^$
)。The file check
-f
requires a valid file system path. But%{REQUEST_URI}index\.html
is not a file system path but a URI path. You can either use-F
instead to check the existence via a subrequest. Or use DOCUMENT_ROOT to build a valid file system path:Furthermore, the other condition can be accomplished with the pattern of
RewriteRule
. As you’re using mod_rewrite in a .htaccess file, the corresponding path prefix is stripped (in case of the document root directory:/
) so that the remaining path is an empty string (matched by^$
).如果您有权访问 httpd.conf(apache 配置文件),您可以在其中设置默认页面。
像这样的东西:
if you have access to httpd.conf (apaches config file) you could set the default page in there.
Something like this:
根据您在更新中发布的规则集,您遇到了一些逻辑错误。现在,您的
RewriteCond
条件之一将始终为 true,因为两个索引文件似乎永远不会存在于同一环境中(一个存在于开发中,另一个存在于生产中)。由于您已将它们进行“或”运算,这意味着您的RewriteRule
永远不会因条件块而被忽略。它很容易修复(我还添加了额外的正斜杠,因为
DOCUMENT_ROOT
通常 没有尾部斜杠):还要注意,您可以设置 虚拟主机 具有本地主机名,以便您的开发和生产在相对路径方面相似。
Based on the rule set that you posted in your update, you have a bit of a logical error going on. Right now, one of your
RewriteCond
conditions will always be true, since it seems likely that both index files will never exist in the same environment (one exists in development, the other in production). Since you've OR'ed them together, this means that yourRewriteRule
will never be ignored due to the condition block.It's simple enough to fix (I've also added additional forward slashes, since
DOCUMENT_ROOT
typically doesn't have a trailing slash):Note too that you could setup a virtual host with a local host name so that your development and production would be similar in terms of relative paths.