How to Remove .html and .php Extension in the URL using .htaccess

Creating our Pages

First, we are going to create our sample pages. Create the ff files and paste the codes below it. index.html
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>How to Remove .html and .php Extension in the URL using .htaccess</title>
  4. </head>
  5. <a href="login">Login</a>
  6. <p>This is the index Page</p>
  7. </body>
  8. </html>
login.php
  1. <?php
  2. echo "This is the Login Page";
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>How to Remove .html and .php Extension in the URL using .htaccess</title>
  9. </head>
  10. <body>
  11. <a href="index">Home</a>
  12. </body>
  13. </html>

Creating our .htaccess File

Lastly, we create our .htacess file. Create a new file, name it as .htaccess and paste the below codes.
  1. <IfModule mod_rewrite.c>
  2. RewriteEngine on
  3.  
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME}.php -f
  6. RewriteRule ^(.*)$ $1.php
  7. #RewriteRule ^([a-z]+)\/?$ $1.php [NC]
  8.  
  9.  
  10. RewriteCond %{REQUEST_FILENAME} !-d
  11. RewriteCond %{REQUEST_FILENAME}.html -f
  12. RewriteRule ^(.*)$ $1.html
  13. #RewriteRule ^([a-z]+)\/?$ $1.html [NC]
  14.  
  15. </IfModule>
That ends this tutorial. Happy Coding :)

Add new comment