Search code examples
.htaccessmod-rewriteclean-urls

Straight Rewrite Rule for htaccess


I was looking for a simple .htaccess configuration that just convert /some_uri to /some_uri.php. Most examples in community are more complicated than I require. I was trying the following, but it didn't work:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(.*)$ ./$1.php

Please help. Thank you.


Solution

  • You need to make sure that the rule does not match itself. In other words, you need to make sure the rule does not match if the url already ends on php. Besides that you probably want to prevent that the rule matches if it already points to a file that exists, so that you can normally serve css/js/images without it trying to append php to it.

    RewriteCond %{REQUEST_URI} !\.php$ #Does not end with .php
    RewriteCond %{REQUEST_FILENAME} !-f #Requested url is not an existing file
    RewriteRule ^ %{REQUEST_URI}.php [L]