Remove website file extensions

.html, .php on an apache webserver

Posted by Tech Grounds on December 12, 2015

Prerequisites


I will be showing you how to remove the file extensions from your website URL to make it look more professional and clean. I will be showing how its done using an Apache webserver so if your using nginx or anything else, this may not help at all. you will also have to have the following software installed and setup:

    Before we begin make sure you are logged in as root. If you do not have root access then add "sudo" before the command.

    How to do it


    1.

    Open up WinSCP, login and go to the folder where all your website pages are stored (step 1). In my case this would be "/var/www/Website". I have shown mine in the image above.


    2.

    The file we are going to add is hidden by default so we have to make it so WinSCP allows us to see hidden files. Press CTRL+ALT+P and the preferences menu should show up. Now go to "Panels" and tick the "Show hidden files" box as I have shown above.


    3.

    Create a file named .htaccess in your websites root folder (where your .html files are) as shown above and paste this code inside the .htaccess file:

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f

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

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

    This will also affect your .php files because the .htaccess is in the root folder of your website.


    4.

    Now that we have the .htaccess file in place we have to change the Apache2 permissions to allow these changes to be made to this directory. Head over to:

    /etc/apache2/apache.conf
    and open apache.conf. Now scroll down until you see:
    Directory /var/www/
    Now change "AllowOverride" from "None" to "All".


    5.

    Before everything starts working you have to type a command into your webservers console which you can connect to using PuTTy or any other SSH client.

    sudo a2enmod rewrite
    Type this into the console and hit enter, "mine shows already enabled" because I have done it before.


    6.

    Lastly, we will have to remove all the .html or .php hrefs in your code. so if you had:

    href="index.html"
    Change it to:
    href="index"
    Now everything should be working and if not then leave a comment below with your problem and ill help troubleshoot it.