将所有子文件夹重定向到根目录中的文件


Redirect all subfolders to a file in root

使用mod重写将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内的子目录重定向或更改为根目录。它呈现了代码,但由于遗漏了css,页面样式完全错误

test.com/style.css

它检查

test.com/no_sub_folder_here/style.css

相反。

此外,经过几次尝试后,它不会将url从no_sub_folder更改为root。因此,过一段时间,我会在我的url中得到一长串no_sub_folder:(

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

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

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