How to make .htaccess work with the current directory and not with subdirectories?

I use mod rewrite to access my PHP files in the root directory of my site indirectly. I currently have something like this,

RewriteRule ^(blog|about|page3|etc)$ /$1.php [L] 

But I would like to use

 RewriteRule ^(.*)$ /$1.php [L] 

So I would not have to update my .htaccess file whenever I want to add a new page to my site. However, the problem is that it also affects my subdirectories. Which makes CSS, javascript, images inaccessible because it redirects to "/dir/example.png.php".

So what is the best solution here?

+2
source share
4 answers

Try the following rule:

 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f RewriteRule .* $0.php [L] 
+3
source

Edit .htaccess as follows:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /$1.php [L] 

After starting the rewrite mechanism, it first checks whether the requested file is an actual file in the system, or if it is actually a directory. If this is not the case, he will execute the rule, which in this case should try to find the file by adding .php and serving it.

Subdirectories, images, CSS, etc. will be okay because they will exit the dubbing after they are discovered.

+2
source

I would do this with filters to ignore subdirectories. Thus, you need to change it only if you add an additional folder. You need to add a line like Sosh if you have images or other files in the folder

The following will ignore the subfolders "images" and "js" and redirect everything else

 RewriteRule ^images* - [L] RewriteRule ^js* - [L] RewriteRule ^(.*)$ /$1.php [L] 
+1
source

Something like this (note!):

 RewriteRule !\.(js|ico|gif|jpg|png|css|zip|gz|html|xml)$ index.php 
-1
source

All Articles