Search code examples
php.htaccess

Ignoring subfolder in URI


I have a problem with .htaccess configuration. I know how to ignore file extensions (e.g. php). That's my code:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.*)$ $1.php

But I want to ignore specific a subfolder in the URI. For example, I have a file in example.com/sites/test.php but in the address bar, I just want to type example.com/test. Any ideas?


Solution

  • You can use:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/sites/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ /sites/$1.php [L]