Search code examples
.htaccess

How can I have 2 rewrite rules on my .htaccess file


I currently have an .htaccess file that has one rewrite rule that removes .html however I cannot seem to be able to do the same thing with .php without messing up my whole website This is the code that I have in my .htaccess file.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.html -f

RewriteRule ^(.*)$ $1.html [NC,L]

Thanks


Solution

  • Sure you can, why not?

    Your version:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    

    Some lightly modified version:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^ %{REQUEST_URI}.html [END]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^ %{REQUEST_URI}.php [END]
    

    You generally should switch off MultiViews when implementing such rules by the way.