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.
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.
. . . . . . # # 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 . . . . . .
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 //your-domain.com/index.php and it will automatically redirect to //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
Related posts:
Joomla has SEO settings that can be changed to make website more optimized for Search engines and make urls more friendly for people. We received a lot of questions about how to optimize urls, make them simple and better displayed. One of that question is how in Joomla remove index.php from url. There are the great setting - Url rewriting, with a help of it we can remove index.php and now we’ll tell you how to do this.
Step 1: Log into Joomla.
To start the process of removing index.php from url on your Joomla website, let's log into admin panel first.
Step 2: Global Configuration.
After you have logged in, find System in the upper left corner, and go to Global Configuration
Step 3: Site tab.
The next step in the Remove index.php from Joomla url algorithm is Select Site tab on the top.
Step 4: URL rewriting.
On the right part of the page find the Use URL rewriting option. Adjust the setting to Yes, and to remove index.php from url in Joomla click Save button.
Step 5: Rename htaccess.
To finish the process and make all work correctly rename htaccess.txt to .htaccess in the root directory.
So now you know how in Joomla template remove index.php from url. Check other Joomla tutorials to find more tips and tricks.