Friday, 7 August 2015

Remove .html or .php extension with .htaccess

.htaccess is a file which will help you to do
  1. Redirect the user to different page.
  2. Password protect a specific directory
  3. Block users by IP
  4. Preventing hot linking of your images
  5. Rewrite URIs
  6. Specify your own Error Documents

Removing Extensions

To remove the .php extension from a PHP file
for example the domain url is example.com/home.php and you want to change intoexample.com/home  so here you have to add the following code inside the .htaccess file:
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^\.]+)$ $1.php [NC,L]
 </IfModule>


To remove the .html extension from a html file
If you want to remove the .html extension from a html file for example example.com/home.htmlto example.com/home you have to add the following code inside the .htaccess file:
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:
<a href="http://www.example.com/services">services</a>

No comments:

Post a Comment