Hiding the .php Extension in the Address Bar
When you deploy a PHP application, the default behavior of most web servers is to serve files exactly as they are named on disk. If your entry point is index.php, the browser's address bar will inevitably display http://www.example.com/index.php. While this accurately reflects the server's file system structure, it can clutter URLs and reveal implementation details that are often unnecessary for the end user. Achieving a clean URL like http://www.example.com/index requires configuring your web server to intercept requests before they reach the PHP engine or to rewrite them transparently.
The mechanism to achieve this depends entirely on your web server stack. The two most common scenarios involve Apache with its .htaccess files and Nginx with its configuration blocks. Understanding the distinction between rewriting a URL and redirecting it is crucial for maintaining SEO integrity. A 301 permanent redirect tells search engines and browsers that the content has moved, whereas a rewrite allows you to serve the same content without changing the browser's location header.
Apache Configuration with Mod_Rewrite
If you are running Apache, the standard approach involves using mod_rewrite. The goal is to tell the server: "If someone requests index, treat it as if they requested index.php." You must ensure that the .htaccess file in your document root (or the main virtual host configuration) contains the necessary directives.
First, verify that mod_rewrite is enabled on your Apache installation. Then, add the following block to your .htaccess file:
RewriteEngine On
RewriteRule ^index$ /index.php [L,QSA]
The RewriteEngine On directive activates the rewrite engine for the directory. The RewriteRule matches the URI path /index. The pattern ^index$ ensures that only the exact string "index" is matched, preventing conflicts with other routes like index.html or index123.
The substitution /index.php tells Apache to internally serve the PHP file. The flags [L,QSA] are critical here. L (Last) stops processing subsequent rules once this one matches. QSA (Query String Append) preserves any query parameters in the URL, such as ?id=5, ensuring they are passed to your script (index.php?id=5).
Important Limitation: If you have a specific requirement where the physical file index.php does not exist and you only want to serve content from index.html (or vice versa), this simple rule might cause a 404 error if the server tries to serve a non-existent file. In that case, you must ensure your directory contains index.php or point the rewrite specifically to a file that exists.
Nginx Configuration
Nginx handles URL rewriting differently than Apache. You cannot place these rules in an .htaccess file because Nginx does not support it; instead, you must edit the main configuration file (usually /etc/nginx/nginx.conf or a site-specific file like /etc/nginx/sites-available/example.com).
To achieve the same result on Nginx, you use the try_files directive inside your location block. This is often more robust for PHP applications because it attempts to serve the directory index first and then falls back to the PHP file if the specific resource isn't found.
location / {
try_files $uri $uri/ /index.php?$query_string;
}
In this configuration, try_files checks if a file or directory with the requested URI exists. If it does, it serves that resource. If not, it defaults to /index.php. The $query_string variable ensures that any GET parameters are appended to the rewritten URL, maintaining functionality for dynamic requests. This method is particularly effective because it avoids the complexity of complex regex patterns often required in Apache's rewrite engine.
Security and SEO Considerations
Hiding the .php extension improves the user experience by presenting cleaner URLs, but it introduces a potential security consideration known as Directory Indexing. By default, when you access http://www.example.com/ without specifying a file, web servers look for an "index" file (usually index.html, index.php, or index.htm). If none exist, they may list the directory contents.
When you rewrite requests to index.php, you must ensure that your PHP application handles the logic of generating content correctly. If your script expects a specific query parameter (like ?page=home) and you simply redirect index to it without passing parameters, your homepage might break. Always test your rewritten URLs with various query strings to ensure the routing logic inside your PHP framework or custom code handles both clean paths and raw paths gracefully.
While tools like Laravel provide robust routing that naturally handles these clean URLs by default, legacy applications require manual configuration as described above. If you are building a new application and want to avoid these configuration hurdles entirely, consider modern frameworks where routing is built into the core logic.