How to remove .php, .html, .htm extensions with .htaccess


I recently wanted to remove the extensions from my website, in order to make the URLs more user and search engine friendly. 

I stumbled across tutorials on how to remove the .php extension from a PHP page. 
What about the .html? I wanted to remove those as well! In this tutorial I’ll show you how to do that easily, by editing the .htaccess file.
If you have not .htaccess file then create own .htaccess file with name .htaccess and  extention .conf


blogpost images msrajawat298
remove extension


Removing Extensions
To remove the .php extension from a PHP file for example yoursite.com/demopage.php to yoursite.com/demopage you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to change the last line from the code above, to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]

RewriteEngine On
# NC makes the rule non case sensitive
# L makes this the last rule that this specific condition will match
# $ in the regular expression makes the matching stop

# Rewrite for hotel_details.php/?id=3
RewriteRule hotel_details/([0-9a-zA-Z]+)$ hotel_details.php?id=$1 [NC,L]
# Rewrite for search_hotel.php
RewriteRule search_hotel search_hotel.php [NC,L]

# for admin pages.......
# Rewrite for admin/index.php
# RewriteRule admin/([0-9a-zA-Z]+)$ admin/index.php [NC,L]

# Rewrite for admin/dasboard.php
RewriteRule admin/dasboard admin/dasboard.php [NC,L]

# Rewrite for admin/add_hotels.php
RewriteRule admin/add_hotels admin/add_hotels.php [NC,L]

# Rewrite for admin/add_industry.php
RewriteRule admin/add_industry admin/add_industry.php [NC,L]

# Rewrite for admin/view_industry.php
RewriteRule admin/view_industry admin/view_industry.php [NC,L]

# Rewrite for admin/view_hotels.php
RewriteRule admin/view_hotels admin/view_hotels.php [NC,L]

# Rewrite for admin/setting.php
RewriteRule admin/setting admin/setting.php [NC,L]

# Rewrite for admin/logout.php

RewriteRule admin/logout admin/logout.php [NC,L]


0 Comments