Search code examples
.htaccesscontent-management-system

htaccess delete and ban all extensions and add / to the end of the URL


I am making a small cms in php. Through htaccess I would like to delete and block all extensions (.php .html. Htm etc.) and add a / at the end of the url. ex. www.miosito/ article-name/

Options -Indexes
DirectoryIndex home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ core.php

this is my current htaccess. Sorry for my bad English and thank you very much in advance


Solution

  • With your shown samples/attempts, could you please try following htaccess Rules file. Please make sure to clear browser cache before testing your URLs.

    RewriteEngine ON
    ##Excluding Indexes and MultiViews options here.
    Options -Indexes -MultiViews
    
    ##Rule for url http://localhost:80/test here.
    RewriteRule ^(test)/?$ $1/category [R=301,L]
    RewriteRule ^(test/category)/?$ $1.php [NC,L]
    
    ##Rule for forbidding html OR htm OR php extension files.
    RewriteRule \.(html|php|Htm)/?$ - [F,NC,L]
    
    ##Rule for adding / at end of urls.
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
    
    ##Rules for non-existing pages to be served here.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ core.php [QSA,L]