Вопрос по .htaccess, mod-rewrite, url-rewriting, apache – мод переписать каталог, если файл / папка не найдена
У меня есть папка с именем & quot; папка1 & quot; в моем корневом каталоге
www.domain.com/ www.domain.com/folder1
Мне нужно перенаправить все запросы на www.domain.com с ошибкой 404 в папку folder1. Вот так:
www.domain.com/a_file.txt
Если a_file.txt не существует, посмотрите в folder1:
www.domain.com/folder1/a_file.txt
Я хочу, чтобы это работало так же для подкаталогов, вот так:
www.domain.com/a_folder (перенаправить, если он не существует в корне)
www.domain.com/folder1/a_folder
Я знаю, что должен использовать RewriteCond% {REQUEST_FILE}! -F, но я не могу этого понять.
1
ответ
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) folder1/$1 [L,R]
The first rewrite-cond ensures you do not loop (in case the file does not exist inside folder1 either
The second one checks that target is not a file
The third - that it is not a folder either
And, finally, rewrite the url. L
flag means this is the last rule applied (even if there are rules after it), R
means redirect. You can also add QSA
flag if you want any query-string parameters passed to the original be sent to the new url