php - .htaccess 如果文件存在则重定向

标签 php apache .htaccess mod-rewrite redirect

我希望根据目录中是否存在文件来使用 .htaccess 重定向到一个页面或另一个页面。

基本上,我需要将所有访问index.php的访问者发送到home.php页面(如果splash.php 不存在)或splash.php页面(如果splash.php 存在) strong> 存在于目录中,到目前为止我有这个...

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} index.php
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^.*$ /splash.php [L]

仅当我明确将 mysite.com/index.php 作为我的 URL 时,才会将访问者发送到 mysite.com/splash.php(如果它存在),如果仅使用 mysite.com URL,则不会工作。

我理解代码的前 3 行,但不理解 !splash.php -f 位(我是从在 SO 上找到的其他重定向代码中得到的)。

如果splash.php不存在,我如何重定向到home.php?

干杯!

最佳答案

试试这个规则。它应该在 mysite.com/index.php、mysite.com/或 mysite.com 上运行

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^(index\.php|/|)$ /splash.php [L]

关于php - .htaccess 如果文件存在则重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6306362/

相关文章:

php - 使用php将文件从其他站点下载到Web服务器

php - 具有多个 INNER JOIN 的 MySQL ORDER BY

php - 如果用户请求子域 WP,则重定向到子域

java - 使用 Apache POI 替换 powerpoint 中的所有文本

php - www 不重定向到非 www .htaccess Wordpress

php - 如何将 PHP 网页转换为 PDF?

apache - 使用 htaccess 限制 IP

ruby-on-rails - Ruby 升级权限错误(Errno::EACCES)

php - 移动媒体 wiki 目录后出现 500 服务器错误

.htaccess - 当非管理员尝试访问 wp-admin 或 wp-login.php 时,如何将用户重定向到 404 页面未找到错误