1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[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