remove index.php from url using .htaccess

How to Remove index.php from URL using .htaccess

By default, PHP-based websites add index.php to the domain name in URLs. It is difficult to remember including it in every URL on your website, and also reveals your technology to potential hackers. So, in this article, we will look at how to remove index.php from URL using .htaccess. You can use these steps to remove index.php from CodeIgniter.


How to Remove index.php from URL using .htaccess

Here are the steps to remove index.php from URL using .htaccess.


1. Enable mod_rewrite

If you have enabled mod_rewrite on your Apache server, you can skip this step. Else open terminal and run the following commands to enable mod_rewrite in Ubuntu/Debian systems.

$ sudo a2enmod rewrite

It takes just one command to enable mod_rewrite in Ubuntu/Debian systems.

For CentOS/RHEL/Fedora systems, open Apache configuration file

$ sudo vi /etc/apache2/httpd.conf

Look for the following line

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning

LoadModule rewrite_module modules/mod_rewrite.so

If you don’t find the above line, just add it to the configuration file.

Look for the following section and change AllowOverride directive from None to All.

. . .
<Directory /var/www/html>
. . .
 # 
 # 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 All
. . .
</Directory>
. . .

Also read : Apache Config File Location


2. Create .htaccess file

Create a blank .htaccess file at your website root /var/www/html

$ sudo vi /var/www/html/.htaccess

Please note that you need to add a dot(.) in .htaccess file name.

Also read : How to Fix “Could not reliably determine Server’s Fully Qualified Domain Name”


3. Update .htaccess configuration

Add the following lines to your .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Save and close the file. Remove index.php file from /var/www/html.

Let us look at the above configuration. %{REQUEST_FILENAME} has the URL part after your domain name. The next three lines basically check if the requested URL is not a regular file (-f) and not a directory (-d) then do a rewrite of the URL excluding index.php from it.

Also read : How to Redirect HTTP to HTTPS on Custom Port in Apache


4. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo systemctl restart httpd

Open browser, and go to http://your-domain.com/index.php and it will automatically redirect to http://your-domain.com.

That’s it. Now you don’t need to include index.php in requested URLs to your website. Also, if anyone accidentally includes index.php in the requested URL it will be automatically removed.

Also read : How to Rsync files between two servers


Leave a Reply

Your email address will not be published. Required fields are marked *