Soley
Soley

Reputation: 1776

Redirect all subfolders to a file in root

There are similar situations and answers here to redirect a url to a specific file using mod-rewrite. I tried them but none of them worked for me.

I would like to redirect all requests to a file and handle everything by that file. It worked fine somehow, but when there are virtual sub-folders, it does not work at all.

For example:

http://test.com/this_page_does_not_exist.php

works fine. but when I use something like this:

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

it does not work fine. It tries to find css files for 404 page from no_sub_folder_here. So what I need to have is to address

http://test.com/no_sub_folder_here

to

http://test.com/

and then load the css for the 404 page. my css is in the root.

Here is my .htaccess file

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] 

it does not redirect or change the directory from sub-directories inside 404 to root. It renders the codes, but the page style is all wrong because of missed css from

test.com/style.css

it checks

test.com/no_sub_folder_here/style.css

instead.

Also, after a couple of tries, it does not change the url form the no_sub_folder to root. So, after a while, I will get a long list of no_sub_folder in my url :(

Upvotes: 1

Views: 72

Answers (1)

Croises
Croises

Reputation: 18671

Because you change the base path, you have to fix that, with:

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

in html <header> of your 404.php page.

Upvotes: 2

Related Questions