Htaccess add .php to everything without extension

I am looking through SO for duplicates, but it's hard for me to find what, in my opinion, should be a simple answer.

What are the rewrite rules for adding .php to something without extension?

examples:

www.website.com/styles.css → www.website.com/styles.css

www.website.com/login → www.website.com/login.php

THANKS!

+4
source share
2 answers

The following may suit your needs:

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

This will automatically add the .php extension to file names without extensions. It may try to rename something.css to something.css.php, but will only do this if something.css.php exists.

+7
source

To extend the answer to @quinxorin, you can also do the following.

 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteCond %{REQUEST_FILENAME}\.php ^(.*)\.php(.*)$ RewriteRule .* %1.php?%2 [QSA] 

This adds the rest of the URL to the result. those. /page/1234 becomes /page.php?1234 .

+2
source

Source: https://habr.com/ru/post/1485646/


All Articles