How To Remove File Extension Using htaccess Tutorial

Reading Time: 3 minutes
1,395 Views

Inside this article we will see the concept i.e How To Remove File Extension Using htaccess Tutorial. Article contains the classified information about How to remove .php, .html extensions with .htaccess.

Whenever you work with URLs, SEO needs a neat and clean URL. Also clean URLs is a key factor to increase User Traffic. So, if you are looking for a solution i.e Remove or Hide File Extension with .htaccess, then article is totally for you to learn and see how to do it.

Read More: How To Convert Associative Arrays into XML in PHP

Let’s get started.

Example #1: Remove .html From URLs Using .htaccess

Suppose you have a website of these pages – index.html, about-us.html, products.html

Right now you don’t have .htaccess file.

So, to access application you need to open the web pages as –

https://example.com/index.html

https://example.com/about-us.html

https://example.com/products.html

Let’s add .htaccess file to application.

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

After this logic, URL will be like this –

https://example.com/index

https://example.com/about-us

https://example.com/products

Example #2: Remove .php From URLs Using .htaccess

Suppose you have a website of these pages – index.php, about-us.php, products.php

Read More: Convert Unix Timestamp To Readable Date Time in PHP

Right now you don’t have .htaccess file.

So, to access application you need to open the web pages as –


https://example.com/index.php

https://example.com/about-us.php

https://example.com/products.php

Let’s add .htaccess file to application.

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

After this logic, URL will be like this –


https://example.com/index

https://example.com/about-us

https://example.com/products

We hope this article helped you to learn How To Remove File Extension Using htaccess Tutorial in a very detailed way.

Read More: How To Check If a Database Table Exists With Laravel

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.