W
W
wakh2017-04-03 23:11:25
htaccess
wakh, 2017-04-03 23:11:25

How to check if a directory exists using .htaccess?

Given.

  1. Hosting nginx + apache 2.2.4
  2. Subdomain, this is important. %{DOCUMENT_ROOT} works "not correctly" in it; it gives the path to the main domain folder. t.wakh.ru (otherwise, the option with the wakh.ru/t subfolder did not work either).
  3. Files to check:
    • /1.html
    • /1.shtml
    • /d1/2.html
  4. .htaccess with the following content
    AddHandler server-parsed .shtml
    
    Options -Indexes +FollowSymLinks +Includes
    RewriteEngine On 
    #RewriteOptions Inherit
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME}	(([^\/]+\/)*[^.]+)\.html
    RewriteCond %1.html 	!-f
    RewriteRule ^ 		1.shtml	[QSA,L,E=VARR:er]


Problem:
  • when requesting t.wakh.ru/d1/2.html, the redirect does not occur, the file /d1/2.html exists - everything is correct
  • when requesting t.wakh.ru/d1/1.html , a redirect occurs, because there is no /d1/1.html file - everything is correct
  • when t.wakh.ru/d2/1.html is requested, the redirect does not occur, although there is no d2 directory, respectively, and the /d2/1.html file too - How to fix this?

Variant with
RewriteCond %{REQUEST_FILENAME}	(([^\/]+\/)*[^.]+)\.html
RewriteCond %2	 	!-d

didn't work.
I found out that RewriteCond %{REQUEST_FILENAME} (([^\/]+\/)*[^.]+)\.html does not work if there is no directory.
If you want to help, feel free to create a similar structure on your server / hosting, in the process of testing on mine, inadequate behavior arose and pressing F5 randomly produced different results.
And another question, in the process of testing, I wanted to use the env|E=[!]VAR[:VAL] flag, but it turns out to be an empty value. Why?
PS
My last question remained unanswered, probably due to confusion.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2017-04-04
@shambler81

1. to make the root document work
2. in the nginx config, find if there is something about .htnl

location  ~* \.(html|htm)$ {
  error_page 404 /404.html;
  expires 30d;

3. all .htaccess to studio including CNC
4. what for RewriteCond %{REQUEST_FILENAME} ?
use RewriteCond %{REQUEST_URI}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question