skip to Main Content

When i make a request "http://domain/index.php" or such "http://domain" i want the server to give me 404 code. When i make "http://domain/some-path/index.php" i want the server to give me an index file from the "some-path" directory. But server give me 404 code in all cases. There my code in htaccsess file.

RewriteEngine On

#RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index.html$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^/some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path/(([^.]+.(html|php))|(assets/.*))$ /$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^some-path/([^/.]+)$ $1.html [L]

It seems that the line RewriteCond %{REQUEST_URI} ^/index.html$ does not work correctly.

2

Answers


  1. Chosen as BEST ANSWER

    This is my answer RewriteEngine On

    RewriteCond %{THE_REQUEST} ^(?!.*(some-path|robots.txt)).*$
    RewriteRule ^ - [L,R=404]
    
    RewriteCond %{REQUEST_URI} ^some-path/?$
    RewriteRule ^ - [L,R=403]
    
    RewriteRule ^some-path/(([^.]+.(html|php))|(assets/.*))$ /$1 [L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^some-path/([^/.]+)$ /$1.php [L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.html -f
    RewriteRule ^some-path/([^/.]+)$ /$1.html [L]
    

  2. With your shown samples and attempts please try following .htaccess rules file.

    • Please make sure to clear your browser cache before testing your URLs.
    • Also make sure to keep your .html files along side with your .htaccess rules file.
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^/$ [OR]
    RewriteCond %{REQUEST_URI} ^index.html$
    RewriteRule ^ - [L,R=404]
    
    RewriteCond %{REQUEST_URI} ^/?some-path/?$
    RewriteRule ^ - [L,R=403]
    
    RewriteRule ^some-path/(([^.]+.(html|php))|(assets/.*))$ /$1 [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^some-path/([^/.]+)$ $1.html [NC,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search