Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

SEO/rewrite rules nginx


My goal is to have the 'nice' urls (ie instead of The hosting company (synthesis) uses an nginx configuration. They told me to send them the .htaccess I would use and they would translate it. However, when that is done, and I try to adjust the settings, I still get a 500 error.

This is what we had sent them to translate. Is there something I'm missing?

RewriteEngine On
RewriteBase /forum/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteRule ^$ index.php/$1

Thank you so much in advance for any help. I'm at a loss here..



  • All you'll need is this:

      location / {
          try_files $uri $uri/ @forum;
      location @forum {
            rewrite ^/(.+)$ /index.php?p=$1 last;

    Also see:

    I also use Nginx and that does work.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited February 2013

    This is what I think it should look like for apache, it has to do with relative path to the root. But it's hard to explain it all here , check this out it may help you get a handle on it.

    < Directory /opt/forum >
    RewriteEngine On 
    RewriteBase /forum/
    RewriteRule ^index\.php$  index.php 
    < /Directory >

    Also here is an online converter you paste the apache htaccess and it converts it to nginx. Maybe you can do it yourself ad see what went wrong.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    this is good convertor I converted the part at the top you put and the nginx result was this

    # nginx configuration
    location / {

    that look like the root / is the default

  • It is much better to understand than rely on converters, you could end up doing something you don't really want to do. No substitute for learning.

    That blog link is useful, though there is much personal to the setup, how it is done. There is also sort of nuances you ca put into your setup.

    The main thing with nginx is to understand why if is limited and doesn't work as expected, so not to use it too much.

    grep is your friend.

  • Okay so according to the host, there's something else in the forum configuration that's not right and I have been attempting to find it without any clue where I should be looking.

    WordPress is installed on the main site.
    Vanilla in directory (/forum)

    When vanilla is installed on this host (the nginx) in the root directory, and I turn on the nice urls, it works fine.

    When it's installed in a directory (/forum) and I turn them on, I get the error.

    The server implemented the redirect rules, but it's still not working and I'm getting this from the server:

    At present the forum is loading without content, and the categories and other forum pages look like they are hitting the forum 404 page, not your standard WordPress 404 nor a 500 error. This leads me to believe that something in the forum configuration may need to be tweaked to allow this to work properly.

    Any ideas? I am desperate to make this work once and for all...

  • GaryFunkGaryFunk Senior Application Developer ✭✭

    You need to make sure the rewrite rules file is in the /forum direcrtory.

  • You need to make sure the rewrite rules file is in the /forum direcrtory.

    Since it's on an nginx that file will never be read (according to the server..)

  • Anyone? Or does anyone have a recommendation on someone I can hire to fix this?

  • GaryFunkGaryFunk Senior Application Developer ✭✭

    You need to have the host put the rewrite rules in the /forum directory.

  • Again it is about understanding what you actually applied.

    Did set up /forum instead of root / you change @forum to the valid rewrite? You can't just copy an paste rule an expect it to work for different things. It is actually fairly logical you point it to where it need to go, you rewrite it to where it is. This is documented. it is not so for removed from Apache rules.

    Also did you reload the nginx configuration?

    grep is your friend.

  • ashabeleashabele New
    edited February 2013

    I am still not having any luck, and I just don't know why. This is what I have:

    Here is what we have implemented for you in the NGINX configuration file:

    New Vanilla Forum Rewrites added 2-13-13

        location /forum {
               rewrite ^/(.+)$ /index.php?p=$1 last;
               #index   index.html index.htm index.php;
               #if (!-e $request_filename) { rewrite . /forum/index.php?p=$request_uri; }
                location / {
                try_files $uri $uri/ /forum;
                index   index.html index.htm index.php;
                if (!-e $request_filename) { rewrite . /index.php last; }
                location ~ .php$ {
                fastcgi_pass   unix:/var/run/php5-fpm.sock ;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        location ~ /\.ht {
                deny  all;
        location ~ \.(jpg|gif|png|ico|jpeg)$ {
                expires 7d;

    What am I missing??

  • x00x00 MVP
    edited February 2013
    # Forum locations
      location ~ ^forum {
          index  index.php;
          try_files $uri $uri/ @forum;
    # rewrite
      location @forum {     
        #pretty urls
            rewrite ^/forum(/.*)?$ /forum/index.php?p=$1 last;

    I'm sorry but if that was you hosts attempt it just goes to show that few hosts are competent nginx specialists.

    put the php handler before this e.g.

      location ~ \.php {
          try_files $uri =404;
          fastcgi_pass unix:/var/run/php5-fpm.sock ;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
          fastcgi_split_path_info ^(.+\.php)(.*)$;

    grep is your friend.

Sign In or Register to comment.