[url rewrite] Shortest code for removing www and file extension, and forcing slash at the end?

Discussion in 'Programming' started by Ian08, Mar 29, 2018.

  1. #1
    I had made my .htaccess to remove www, remove file extension and force slash at the end. The code now looks like:
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [NE,L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [L,R=301]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+)/$ $1.php [L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
    RewriteRule ^ /%1/ [L,R=301]
    Code (ApacheConf):
    It is working as expected. But because the code actually consists of different pieces I obtained from multiple sources, I'm not sure if it is doing something redundant such as unnecessary redirections.

    Any advice would be mush appreciated.
     
    Last edited: Mar 29, 2018
    Ian08, Mar 29, 2018 IP