如何重定向“/”到“/home.html”仅当文件“/index.html”存在时物理上不存在吗?

发布于 2024-09-19 03:35:36 字数 1052 浏览 5 评论 0原文

我找到了一种将“/”重定向(不是加载,而是更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇女薄笑 2024-09-26 03:35:36

文件检查 -f 需要有效的文件系统路径。但 %{REQUEST_URI}index\.html 不是文件系统路径,而是 URI 路径。您可以使用 -F 来通过子请求检查是否存在。或者使用DOCUMENT_ROOT来构建有效的文件系统路径:

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

此外,其他条件可以通过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:

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

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 ^$).

雪化雨蝶 2024-09-26 03:35:36

如果您有权访问 httpd.conf(apache 配置文件),您可以在其中设置默认页面。

像这样的东西:

<IfModule dir_module>  
    DirectoryIndex index.html home.html
</IfModule>

if you have access to httpd.conf (apaches config file) you could set the default page in there.

Something like this:

<IfModule dir_module>  
    DirectoryIndex index.html home.html
</IfModule>
罪歌 2024-09-26 03:35:36

根据您在更新中发布的规则集,您遇到了一些逻辑错误。现在,您的 RewriteCond 条件之一将始终为 true,因为两个索引文件似乎永远不会存在于同一环境中(一个存在于开发中,另一个存在于生产中)。由于您已将它们进行“或”运算,这意味着您的 RewriteRule 永远不会因条件块而被忽略。

它很容易修复(我还添加了额外的正斜杠,因为 DOCUMENT_ROOT 通常 没有尾部斜杠):

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteCond %{DOCUMENT_ROOT}/domain.tld/www/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

还要注意,您可以设置 虚拟主机 具有本地主机名,以便您的开发和生产在相对路径方面相似。

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 your RewriteRule 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):

RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteCond %{DOCUMENT_ROOT}/domain.tld/www/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文