This guide shows you how to remove index.php from the address bar on an Apache web server, so URLs like example.com/index.php/page become clean, memorable addresses like example.com/page. This is a common requirement for PHP frameworks (Laravel, CodeIgniter, WordPress) and custom applications that route all requests through a single front controller. You’ll need access to your Apache configuration or an .htaccess file, and the Apache mod_rewrite module must be enabled.
What You’ll Need
- Access to your Apache server configuration (httpd.conf, apache2.conf, or a virtual host file) or an
.htaccessfile ifAllowOverride Allis set. - SSH or file manager access to edit files on the server.
- Apache’s
mod_rewritemodule enabled. You can check withapache2ctl -M(Linux) orhttpd -M(Windows) and look forrewrite_module. - Basic understanding of regular expressions (optional but helpful).
Step-by-Step
- Enable mod_rewrite (if not already enabled). On Debian/Ubuntu:
sudo a2enmod rewrite. On CentOS/RHEL, it’s usually enabled by default. Restart Apache after enabling:sudo systemctl restart apache2orsudo service httpd restart. - Locate the correct configuration file. If you have access to the virtual host file (often in
/etc/apache2/sites-available/or/etc/httpd/conf.d/), edit that. Otherwise, create or edit an.htaccessfile in your web root (the directory whereindex.phpresides). - Ensure
AllowOverrideis set toAll(for .htaccess method). In the virtual host or main config, find the<Directory>block for your web root and setAllowOverride All. If you’re editing the virtual host directly, you can skip this step. - Write the rewrite rules. Add the following to your
.htaccessfile or inside the<Directory>block in the virtual host:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This tells Apache: if the requested file or directory does not exist on disk, rewrite the URL to index.php/ followed by the original path. The [L] flag stops processing further rules.
- Adjust for subdirectory installations. If your app runs in a subdirectory like
example.com/myapp/, add aRewriteBasedirective. Place this line just afterRewriteEngine On:
RewriteBase /myapp/
And modify the rewrite rule to:
RewriteRule ^(.*)$ index.php?/$1 [L]
Note the ? after index.php – this prevents Apache from appending the query string incorrectly in some configurations.
- Save the file and test. If you edited the virtual host, restart Apache:
sudo systemctl restart apache2. If you used.htaccess, no restart is needed – changes take effect immediately. - Update your application’s base URL. If your PHP app generates links with
index.php, you may need to configure it to use clean URLs. For example, in CodeIgniter, set$config['index_page'] = '';inconfig.php. In Laravel, ensure theAPP_URLin.envpoints to the clean URL.
Common Mistakes
- Forgetting to enable mod_rewrite. Without it, the rules are ignored and you’ll see 404 errors.
- Using
.htaccesswhenAllowOverrideisNone. The rules will be silently ignored. Check your virtual host configuration. - Not including the
RewriteCondlines. If you omit them, Apache will rewrite existing files (like CSS, JS, images) toindex.php, breaking your site’s styling and scripts. - Incorrect path in
RewriteBase. If your app is in a subdirectory and you forgetRewriteBase, the rewrite may go to the wrong location. - Using
index.phpin the rule without a leading slash or proper context. In a virtual host context, the rule should beRewriteRule ^(.*)$ /index.php/$1 [L](with a leading slash). In.htaccess, no leading slash is used.
Tips
- Test with a simple PHP file first. Create a
test.phpthat printsphpinfo()and try to access it viaexample.com/testto verify the rewrite works before applying it to your full app. - Use absolute paths in
RewriteRulewhen in virtual host. For clarity, useRewriteRule ^(.*)$ /index.php/$1 [L]to avoid confusion. - Check your framework’s documentation. Most frameworks have specific instructions for removing
index.php. For example, Laravel ships with a public/.htaccess that already does this; you just need to enable mod_rewrite. - Clear your browser cache. Sometimes old redirects or cached pages can make it seem like the rewrite isn’t working.
- Use
RewriteLog(Apache 2.2) orLogLevel alert rewrite:trace6(Apache 2.4) to debug. This will show you exactly what Apache is doing with each request.
Troubleshooting
If you get a 404 error: Check that the rewrite rules are being applied. Look at the Apache error log (/var/log/apache2/error.log or /var/log/httpd/error_log) for clues. Also verify that mod_rewrite is enabled and AllowOverride is correct.
If you get a 500 Internal Server Error: This usually means a syntax error in your .htaccess or configuration. Check for typos, missing spaces, or incorrect flags. A common cause is having RewriteEngine On multiple times or a rule that loops infinitely.
If static files (CSS, JS) are broken: Ensure the RewriteCond lines are present. If they are, the issue might be that your application generates absolute paths that include index.php. Update your base URL configuration.
If only the homepage works: Your rule might be too restrictive. Try using RewriteRule ^(.*)$ index.php?/$1 [L] (with the question mark) to avoid query string issues.
If the rewrite works but URLs show index.php when you click links: This is a client-side issue – your application is generating links with index.php. Update your app’s base URL or route configuration.
Frequently Asked Questions
What does mod_rewrite do?
mod_rewrite is an Apache module that allows you to rewrite URLs on the fly. It’s the engine that powers the RewriteRule directives used to remove index.php.
Is .htaccess the only way?
No. You can put the same rules in the virtual host configuration, which is actually preferred for performance because .htaccess files are read on every request. However, .htaccess is easier if you don’t have access to the server config.
Will this affect my existing links with index.php?
No. The rewrite rules only affect requests that do not match an existing file or directory. If someone requests example.com/index.php/page, it will still work because index.php exists as a file.
Can I remove index.php from the URL without rewriting?
You can’t remove it from the address bar without some form of rewriting or routing, because the web server needs to know which file to execute. Rewriting is the standard approach.
Why do I get a 404 when I access example.com/page but example.com/index.php/page works?
This means your rewrite rules are not being applied. Check that mod_rewrite is enabled and that your .htaccess is in the correct directory. Also ensure AllowOverride is set to All.
Do I need to change anything in my PHP code?
Usually yes. Your application should generate URLs without index.php. Most frameworks have a setting for this. If you’re using a custom router, you may need to adjust how you build links.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- China’s IPv9 Decimal Network Project Explained
- Fix Wired Earbuds That Only Work in One Ear
- What to Check Before Buying the Dip in Indian Shares
- Cool Raspberry Pi Projects: 15 Ideas for Any Skill Level3
- Terraform vs Kubernetes: Key Differences
- Internet Connectivity in Remote Areas: Options0.80 USD/GB