The .htaccess file is a powerful tool for configuring and customising the Apache web server. Among other things, it can be used to redirect URLs to the corresponding PHP files, making it a useful tool for creating clean, search engine friendly URLs. In this article, we’ll explore how to use the .htaccess file to achieve this.

To get started, first make sure that your web server is running the Apache web server software. The .htaccess file is an Apache configuration file and is not supported by all web servers.

Assuming you are using Apache, create a file named “.htaccess” in the root directory of your website. If you’re using a hosting service like cPanel, you can create the file using the file manager. If you’re using an FTP client, you can create the file in the root directory of your website.

Next, add the following code to the .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/ # These two lines ignore the index URL
RewriteCond %{REQUEST_URI} !  #
RewriteRule ^(.*) $1.php

This code turns on the rewrite engine and tells Apache to redirect requests for URLs that match the pattern “yourdomain.com/page1” to the corresponding PHP file “page1.php”.

Let’s break down the code. The first line, “RewriteEngine On”, turns on the rewrite engine, which is necessary for URL rewriting. The second line, “RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php”, is the actual rewrite rule. The “^([a-zA-Z0-9_-]+)$” pattern matches any alphanumeric characters, as well as hyphens and underscores. The “$1.php” part of the rule is the replacement string. The “$1” refers to the captured string in the pattern, which is the page name, and the “.php” is added to the end to redirect the request to the PHP file.

Now, if you navigate to “yourdomain.com/page1”, Apache will redirect the request to “page1.php”. You can also add query strings to the URL, like “yourdomain.com/page1?id=123”, and Apache will still redirect the request to “page1.php”.

It is important to note that this type of URL rewriting is case-sensitive. If you have a page named “Page1” instead of “page1”, the redirect will not work. To make the rewrite rule case-insensitive, you can add the “NC” flag to the end of the rule, like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/ # These two lines ignore the index URL
RewriteCond %{REQUEST_URI} !  #
RewriteRule ^(.*) $1.php [NC]

With the “NC” flag added, the rewrite rule will be case-insensitive.

In addition to redirecting URLs to PHP files, the .htaccess file can be used to create more complex rewrite rules, such as redirecting specific URLs to different pages or redirecting all URLs to a single page. However, it’s important to use the .htaccess file with caution, as incorrect or poorly written rules can cause errors and negatively impact your website’s performance.

By using the RewriteRule function, you can create clean, search engine-friendly URLs that are easy to read and remember. However, it’s important to use the .htaccess file with caution and to test all rewrite rules thoroughly to avoid causing errors or negatively impacting your website’s performance.

Let's Get In Touch!


Ready to start your project? Get in touch to have a chat!