How to remove extension in url through (.htaccess) file

May 30, 2017 · 1 min read

Removing extension from urls have advantage that it’s hide the backend technology from end users.
First step is to create a .htaccess file on your document root and write these codes to remove file extensions.

Your current url look like this :

You need to change the url on your document from http://yoursite.com/demo.php to http://yoursite.com/demo
and write these codes in .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

Now turn on the rewrite_module of your Server.

Now, Your Url is changed and it look like  :

How to redirect www to non www url with .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
RewriteRule ^www\.(.*)$ https://$1 [L,R=301]

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://techlifediary.com/$1 [R,L]

That’s it! hope you guys like this post and please don’t forget to share.

How to turn On rewrite module in WampServer
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Pin on Pinterest
Pinterest