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!
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With