User Agents

Web browsers and everyday web tech, explained

Remove File Extensions from URLs: Apache & Nginx

· 6 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Removing file extensions like .php, .html, or .asp from URLs makes your site cleaner, easier to remember, and more professional. Instead of example.com/about.php, visitors see example.com/about. This guide shows you how to achieve this on Apache (using .htaccess) and Nginx (using server configuration). You'll need access to your server's configuration files and the ability to restart the web server.

What You'll Need

  • Access to your server's file system (via SSH, FTP, or hosting control panel).
  • For Apache: permission to use .htaccess files (usually enabled by default) or access to the main Apache configuration file (httpd.conf or apache2.conf).
  • For Nginx: access to the server block configuration file (often located in /etc/nginx/sites-available/ or /etc/nginx/conf.d/).
  • A text editor (like nano, vim, or Notepad++).
  • Basic understanding of regular expressions (optional but helpful).

Step-by-Step

For Apache (using .htaccess)

  1. Locate or create the .htaccess file. In your website's root directory (where your main index.php or index.html resides), check if a file named .htaccess exists. If not, create one. The file name starts with a dot and has no extension.
  2. Enable the rewrite engine. Open the .htaccess file and add the following line at the top (if not already present):
    RewriteEngine On
  3. Add the rewrite rule to remove extensions. Append this rule to the file:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+?)\.php$ /$1 [L,R=301]
    This rule does the following: it checks if the requested URL does not point to a real directory, and if a corresponding .php file exists. If both conditions are met, it redirects the URL with .php to the version without it. For example, /about.php becomes /about.
  4. Handle internal rewriting (so the page still works). Add another rule to internally rewrite URLs without extensions back to the actual file:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+?)$ /$1.php [L]
    This tells Apache: if the requested URL is not a directory and a corresponding .php file exists, serve that file without changing the URL in the browser.
  5. Repeat for other extensions (like .html). If you also want to remove .html, duplicate the rules and replace \.php with \.html and .php with .html in the rewrite target.
  6. Save and test. Save the .htaccess file. Apache reads this file on every request, so no restart is needed. Test by visiting a URL like yourdomain.com/about – it should load the corresponding about.php file.

For Nginx

  1. Open your server block configuration. Find the configuration file for your site (e.g., /etc/nginx/sites-available/example.com). Edit it with a text editor.
  2. Add a location block for extensionless URLs. Inside the server { } block, add the following:
    location / {
        try_files $uri $uri.php $uri.html =404;
    }
    This tells Nginx to first try the exact URI as a file, then try with .php appended, then with .html, and if none exist, return a 404 error.
  3. Add a redirect for old URLs with extensions (optional but recommended). To redirect visitors who still use the old .php URLs, add:
    location ~ \.php$ {
        return 301 $scheme://$host$uri_without_extension;
    }
    However, this is tricky because $uri_without_extension is not a built-in variable. A simpler approach is to use a rewrite:
    rewrite ^/(.+)\.php$ /$1 permanent;
    Place this inside the server block, before any location blocks. This will redirect any URL ending in .php to the same URL without the extension.
  4. Test the configuration. Run nginx -t to check for syntax errors.
  5. Reload Nginx. Apply the changes by running systemctl reload nginx (or service nginx reload depending on your system).
  6. Test. Visit yourdomain.com/about and ensure it loads the about.php content.

Common Mistakes

  • Forgetting to handle both redirect and internal rewrite. On Apache, if you only add the redirect rule, the URL will change but the page will break (404). You need both rules.
  • Using the wrong regex. In Apache, the pattern ^(.+?)\.php$ uses a lazy match. If you use ^(.+)\.php$ (greedy), it may still work but can cause issues with nested paths. Stick to the lazy version.
  • Not checking for existing files. If you don't include the RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME}\.php -f conditions, you might accidentally rewrite URLs that don't correspond to real files, causing 404s.
  • Forgetting to reload Nginx. Unlike Apache, Nginx does not read config changes automatically. You must reload or restart it.
  • Not considering other extensions. If you have both .php and .html files, you need to handle both in the rules, otherwise some URLs will break.

Tips

  • Use 301 redirects for old URLs. This tells search engines that the page has permanently moved, preserving SEO rankings.
  • Keep your file names consistent. If you have about.php, make sure the URL /about maps to it. Avoid having both about.php and about.html – it will cause conflicts.
  • Test on a staging environment first. If possible, try the configuration on a test server to avoid breaking your live site.
  • Use a plugin or CMS feature if available. Many CMSs like WordPress have plugins that handle this automatically. But for custom sites, these manual rules are the way to go.
  • Consider trailing slashes. Some prefer /about/ instead of /about. You can adjust the rules to add a trailing slash if you prefer.

Troubleshooting

  • Getting a 404 error after the change. First, check that the file actually exists. For Apache, verify that the .htaccess file is in the correct directory and that the rewrite module is enabled (a2enmod rewrite). For Nginx, run nginx -t to ensure your config is valid.
  • Redirect loop (too many redirects). This often happens if you have both a redirect rule and an internal rewrite that conflict. Ensure that the redirect rule only applies to URLs that actually end with .php, and the internal rewrite only applies to URLs without extensions. In Apache, the conditions should prevent loops.
  • URLs with query strings break. The rules above do not handle query strings (e.g., /page?param=1). If you need to preserve query strings, add [QSA] to the rewrite rule in Apache: [L,R=301,QSA]. For Nginx, the try_files automatically preserves query strings.
  • .htaccess not working. Check if your Apache configuration has AllowOverride All for the directory. If not, you need to use the main config file instead.
  • Nginx not applying changes. Make sure you are editing the correct server block. Check with nginx -T to see the effective configuration.

Frequently Asked Questions

Will this work for all file types?

Yes, you can adapt the rules for any extension like .asp, .aspx, or .jsp. Just replace the extension in the rules accordingly.

Do I need to change my links in my HTML?

No, the rewrite rules handle it. You can keep your internal links pointing to about.php; the server will redirect to /about and serve the content. However, it's good practice to update your links to the clean URLs for consistency.

Will this affect my SEO?

If you use 301 redirects, it should not harm your SEO. In fact, clean URLs are often considered better for user experience and can slightly improve click-through rates.

Can I remove extensions from URLs that have parameters?

Yes, but you need to ensure the query string is preserved. In Apache, add [QSA] to the rewrite rule. In Nginx, the try_files directive automatically preserves query strings.

What if I have both .php and .html files with the same name?

This will cause a conflict. The server will prioritize one over the other based on the order of rules. To avoid issues, rename one of the files so they don't share the same base name.

Is there a way to do this without editing server config?

If you use a CMS like WordPress, you can install a plugin that handles URL rewriting. For static sites, you might use a build tool that generates clean URLs. But for custom server setups, editing config is the standard method.