Custom routes – Htaccess tutorial
In this htaccess tutorial, you will learn how to create custom routes. You will also learn how you can prevent file listing using htaccess.
Video tutorial:
You need to create a file named “.htaccess” (note the dot at the start) at the root of your project. To prevent directory listing, write the following line in the “.htaccess” file.
Options -Indexes
Before using any routing rules and conditions, we must first activate them. Write the following line to activate the rewrite rules.
Options -Indexes
RewriteEngine On
Then we need to make sure the requests do not match any directory, file name, or symbolic link.
The following line checks if the requested file name is not a directory.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
The following line checks if the requested file name is not a file.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
The following line checks if the requested file name is not a symbolic link.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
Example 1:
Now, let’s say you want to redirect all the requests from the “users/list” route to the “users-list.php” file. You can do that by adding the following line in your htaccess file.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^users/list$ users-list.php [L]
^ means the start of the string.
$ means the end of the string.
[L] means to stop looking for more RewriteRule if this condition matches.
Example 2:
Now let’s say you want to redirect all requests from the “users/fetch/1” route to the “user-by-id.php” file. Here, the last “1” is the parameter you want to send. Now to redirect the request to the file, you need to add the following rule:
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^users/list$ users-list.php [L]
RewriteRule ^users/fetch/([0-9]+)$ user-by-id.php?id=$1 [QSA,L]
([0-9]+) will match for any integer number.
?id=$1 means that it will send the parameter named “id“, with the value matched from the previous regular expression, to the file “user-by-id.php“.
QSA (Query String Append) will make sure to append the existing query string (if any) while preserving the additional parameters (id=1 in this case) to the URL.
Then in the “user-by-id.php” file, you can get the ID using the following code:
<?php
echo $_GET["id"];
?>
Example 3:
For instance, if you want to redirect the URL “users/fetch/adnan” (‘adnan’ is the argument) to the file “user-by-name.php“, you can do that by adding the following rewrite rule.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^users/list$ users-list.php [L]
RewriteRule ^users/fetch/([0-9]+)$ user-by-id.php?id=$1 [QSA,L]
RewriteRule ^users/fetch/([a-zA-Z\s-]+)$ user-by-name.php?name=$1 [QSA,NC,L]
([a-zA-Z\s-]+) This regular expression will match all alphabets, including multiple words, letters, spaces, and hyphens.
NC This flag will make the match case insensitive.
Then in the “user-by-name.php” file, you can get the name using the following code:
<?php
echo $_GET["name"];
?>
Following is the complete code of the “.htaccess” file.
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^users/list$ users-list.php [L]
RewriteRule ^users/fetch/([0-9]+)$ user-by-id.php?id=$1 [QSA,L]
RewriteRule ^users/fetch/([a-zA-Z\s-]+)$ user-by-name.php?name=$1 [QSA,NC,L]
Download:
If you are running on HTTP, you can make your web server redirect the HTTP requests to HTTPS following this tutorial.
I also created a simple and lightweight PHP MVC framework using this htaccess rewrite condition. You can check it out here.
This concludes our htaccess tutorial on creating custom routes. If you face any issues with this, kindly let me know.