How to rewrite the .htaccess file to hide all *.php & *.html extensions Print

  • 353

Remove .php & .html from URL in sub-directory

Add the below lines to the .htaccess file:-

To remove .php extension use:-

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]


OR

Options -MultiViews
RewriteEngine on

# remove php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# rewrite with php php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sub/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

To remove .html extension use:-

#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

 


Was this answer helpful?

« Back

Powered by WHMCompleteSolution