将所有子文件夹重定向到root中的文件

时间:2015-11-26 18:36:23

标签: php .htaccess http

这里有类似的情况和答案,使用mod-rewrite将URL重定向到特定文件。我试过了,但没有一个能为我工作。

我想将所有请求重定向到文件并按该文件处理所有内容。它运行得很好,但是当有虚拟子文件夹时,它根本不起作用。

例如:

http://test.com/this_page_does_not_exist.php

工作正常。但是当我使用这样的东西时:

http://test.com/no_sub_folder_here/this_page_does_not_exist.php

它不能正常工作。它试图从no_sub_folder_here中找到404页面的css文件。所以我需要的是解决

http://test.com/no_sub_folder_here

http://test.com/

然后加载404页面的CSS。我的CSS在根目录中。

这是我的.htaccess文件

RewriteEngine On

# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]

# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward it to 404.php in current directory
RewriteRule . %{ENV:BASE}/404.php [L] 

它不会将目录从404内的子目录重定向或更改为root。它呈现代码,但由于错过了来自

的CSS,页面样式都是错误的
test.com/style.css

检查

test.com/no_sub_folder_here/style.css

代替。

此外,经过几次尝试,它不会将no_sub_folder的url格式更改为root。所以,过了一会儿,我会在我的网址中找到一长串no_sub_folder :(

1 个答案:

答案 0 :(得分:2)

因为您更改了基本路径,所以必须使用以下命令修复它:

<base href="http://test.com/">

<header>页面的html 404.php中。

相关问题