Nginx rewrite rules for SEO friendly URL structure – WordPress and another Web application

I have got WordPress installed on my root example.com/ Website.

Also I have another Web application that on example.com/my-app/ which has got ugly links like example.com/my-app/file.php?arg=value to access.

Read More

How can I make my-app SEO friendly URI structure working as example below?:

example.com/my-app/file.php?arg=value
to
example.com/my-app/value/

Currently, if I access to example.com/my-app/value/ it redirects me to my WordPress post which contains some characters of the /value/. Why? Is there anyway to get this working?

My Nginx directives for domain are listed below:

location / {
  try_files $uri $uri/ /index.php?$args;
  fastcgi_read_timeout 300;
}

location ~ .php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Any ideas? Should I use Ajax navigation, with switch cases? What about HTML5 pushState() API? Any working examples?

Thanks for help!

Related posts

1 comment

  1. Both WordPress and my-app work within a common document root which makes things simple.

    The last element of the try_files directive is the default action for (for example) SEO friendly URLs.

    You need a different default handler for URIs which begin with /my-app, which is achieved using a location /my-app block, for example:

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location /my-app {
        try_files $uri $uri/ /my-app/file.php?arg=$uri&$args;
    }
    location ~ .php$ {
        try_files $uri =404;
        ...
    }
    

    In the above case, arg is set to the value /my-app/value. If you really must extract the last part of the URI, add a named location with a rewrite, for example:

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location /my-app {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/my-app/(.*)$ /my-app/file.php?arg=$1;
    }
    location ~ .php$ {
        try_files $uri =404;
        ...
    }
    

    Note that the fastcgi_read_timeout 300; (in your question) needs to be placed in the location ~ .php$ block, or in one of the outer blocks, in order to be effective.

    See this for details of the nginx directives used above.

Comments are closed.