How to access web page without file extension - Apache .htaccess URL rewrite

Reference Server

In this tutorial we will learn to load a file without using its file extension. For this we will configure Apache server and we will create .htaccess file.

Quick info

Document Root

Document Root is the folder or directory on the hosting server where all the web pages and other assets like image files, stylesheets and JavScript files etc are kept. And when a user visits the website she is actually accessing this folder.

In short: This directory holds our website.

The Document Root directory on a Linux type server is generally set to
/var/www/html

.htaccess file

.htaccess is a configuration file for web servers running Apache. It is a special type of file and is commonly used for URL rewriting, redirecting to other URL, preventing access etc.

The scenario

Consider our website URL is https://www.example.com

And let us consider that the Document Root contains the following files.

 /
 |
 |
 +--api/
 |  |
 |  |
 |  +-- helloworld.php
 |
 |
 +-- index.php

So, we have an api folder and an index.php file inside the Document Root folder. And inside the api folder we have a helloworld.php file.

Now, in order to access the index.php file from the web browser we can type the following URL.

https://www.example.com/index.php

And, in order to access the helloworld.php file, which is inside the api folder, we have to type the following URL.

https://www.example.com/api/helloworld.php

Note! If we use the following URL in the browser then we will get error and the file will not load.

https://www.example.com/index
https://www.example.com/api/helloworld

How to load file without file extension?

In order to solve this we have to make few changes on the server side.

Change in httpd.conf file

Login to your server via Terminal using SSH key or username and password.

You will find the httpd.conf file in the following directory.
/etc/httpd/conf

Use the following command in the terminal to go to that directory.

[root@example ~]# cd /etc/httpd/conf

Once inside the conf directory you will see the httpd.conf file. Use the ls -la command.

[root@example conf]# ls -la

Output

drwxr-xr-x 2 root root  4096 Jan 01 10:01 .
drwxr-xr-x 5 root root  4096 Jan 01 10:01 ..
-rw-r--r-- 1 root root 11776 Jan 01 10:02 httpd.conf

Make a backup copy of the file using the following command.

[root@example conf]# cp httpd.conf httpd.conf.backup

Making a backup file is useful in case you want to revert back.

The above command will create a httpd.conf.backup file in the conf folder.

[root@example conf]# ls -la

Output

drwxr-xr-x 2 root root  4096 Jan 01 10:01 .
drwxr-xr-x 5 root root  4096 Jan 01 10:01 ..
-rw-r--r-- 1 root root 11776 Jan 01 10:02 httpd.conf
-rw-r--r-- 1 root root 11753 Jan 01 10:10 httpd.conf.backup

Now open the httpd.conf file using vi. And make sure you have the rights to modify the file. Use sudo if necessary.

[root@example conf]# vi httpd.conf

Or, if your are using sudo.

[root@example conf]# sudo vi httpd.conf

This will open the httpd.conf file in the vi editor. Now scroll down till you find this line.

<Directory "/var/www/html">

Now press the i key to switch to INSERT mode.

Comment out the line AllowOverride None using the # hash sign.

#AllowOverride None

And now, write the following line after it.

AllowOverride All

Now you will have something like the following.

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn&apost give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

Now, press the ESC key to come out of the INSERT mode and the type :wq to save the file and exit.

You will see the command prompt. Type the following command to restart apache.

[root@example conf]# systemctl restart httpd

Or, use sudo if required.

[root@example conf]# sudo systemctl restart httpd

Now .htaccess

Now move to the Document Root directory using the following command.

[root@example conf]# cd /var/www/html

Once inside the html (Document Root) directory create a .htaccess file using the vi command.

[root@example html]# vi .htaccess

Or, use sudo if required.

[root@example html]# sudo vi .htaccess

The vi editor will open. Press i key to switch to INSERT mode and write the following.

<IfModule mod_rewrite.c>
#this will start rewrite engine
RewriteEngine On

#this will set the base directory
RewriteBase /
</IfModule>

Now, press ESC to come out of INSERT mode and then type :wq to save the file and exit.

So, in the above file we have turned ON the rewrite engine and now the files should open without extension.

Now use the following URL in the browser to access the files without file extension.

https://www.example.com/index
https://www.example.com/api/helloworld