FallbackResource重定向问题求助:避免根域名被重定向至/user.php
解决根域名被FallbackResource意外重定向的问题
嘿,这个问题我之前也踩过坑!核心原因是FallbackResource会在服务器找不到请求的资源时触发,当你访问根域名(incupe.com)时,如果服务器没明确把index.php列为默认索引文档,就会判定根请求的“资源不存在”,进而触发重定向到user.php。
下面给你两个靠谱的解决方案,选一个适合你的就行:
方案一:指定默认索引文档(最简单)
在你的.htaccess里加一行DirectoryIndex配置,明确告诉服务器优先加载index.php作为根目录的默认页面:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working RewriteBase / # 指定默认索引文档,确保根域名访问时直接加载index.php DirectoryIndex index.php # Always use https for secure connections # Replace 'www.example.com' with your domain name # (as it appears on your SSL certificate) RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.incupe.com/$1 [R=301,L] Options All -Indexes FallbackResource /user.php
修改后,访问incupe.com时服务器会直接返回index.php,不会触发FallbackResource;只有当请求的文件/目录确实不存在时,才会跳转到user.php。
方案二:用mod_rewrite替代FallbackResource(更灵活)
如果觉得FallbackResource的行为不够可控,改用mod_rewrite规则能精确筛选需要重定向的请求:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working RewriteBase / # Always use https for secure connections # Replace 'www.example.com' with your domain name # (as it appears on your SSL certificate) RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.incupe.com/$1 [R=301,L] Options All -Indexes # 仅当请求的文件/目录不存在,且不是根请求时,转发到user.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/$ RewriteRule ^(.*)$ /user.php [L]
规则解释:
%{REQUEST_FILENAME} !-f:请求的不是已存在的文件%{REQUEST_FILENAME} !-d:请求的不是已存在的目录%{REQUEST_URI} !^/$:请求的不是根路径(/)
满足以上三个条件时,才会把请求转发到user.php,完美避开根域名的意外重定向。
先试试方案一,它最省心;如果不行再换方案二,肯定能解决你的问题!
内容的提问来源于stack exchange,提问作者Boon Hock Teh
相关产品推荐
相关产品推荐

