• Questions
  • Add Rewrite Rule for URLs ending with .php

I redesigned and took over a site, the old site used a really old CMS, and all its URLs ended in .php so now I'm getting lots of indexing issues with search engines since Nginx is configured to 404 anything that ends in .php and not index.php. I get the nginx 404 not the WordPress 404 since nginx is just blocking direct access to a php file.

Are there any issues with just adding a rewrite rule in the nginx config to just simply remove the .php from the URL and then letting WordPress handle it from there?

Example of what I want to add:

location ~ ^/(?!index\.php$)(.+)\.php($|/) {
        rewrite ^/(?!index\.php$)(.+)\.php$ /$1 permanent;
    }

I plan to put it in the site's nginx config that WordOps provides after the index but before any includes.

Thanks

    Tricky - you might hit problems with some WordPress urls, e.g. wp-login.php, but you could add exclusions.
    I'd also use a return 301 rather than a rewrite.

    So something more like this (untested):

    location ~ ^/(?!wp-login\.php$|wp-admin/|wp-includes/|index\.php$)(.+)\.php$ {
        return 301 /$1;
    }

    But if you can get a list of old urls then using a map might be better/safer e.g. (also untested):

    map $request_uri $new_uri {
        default "";
        /url1.php /new-path-1;
        /url2.php /new-path-2;
        # etc
    }
    server {
        ...
    
        # Redirect old url.php requests to new paths
        location ~ ^/url[0-9]+\.php$ {
            if ($new_uri) {
                return 301 $new_uri;
            }
            return 404;
        }
    
    etc

    kevinjmcmahonjr I plan to put it in the site's nginx config that WordOps provides after the index but before any includes.

    Bear in mind processing order in Nginx is based on match type first of all, which also applies to includes. Everything is treated as one big file so e.g. Exact match ('=') will always override '~' even if in a later include.

    Hosted by VirtuBox