Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess add .php to everything without an extension already

I'm looking around SO for duplicates, but I'm having trouble finding what I think should be a simple answer.

What are the rewrite rules to add .php to anything without an extension already?

examples:

www.website.com/styles.css -> www.website.com/styles.css

www.website.com/login -> www.website.com/login.php

THANKS!

like image 706
pinhead Avatar asked Jun 11 '13 16:06

pinhead


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 filenames without extensions. It may attempt to rename something.css to something.css.php, but will only do so if something.css.php exists.

like image 72
IanPudney Avatar answered Nov 16 '22 23:11

IanPudney


To expand on the answer by @quinxorin you could also do the following.

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

This appends the remainder of the url to the result. ie /page/1234 becomes /page.php?1234.

like image 5
Sam Avatar answered Nov 16 '22 23:11

Sam