Question

Enabling Nginx mod_rewrite

I just moved a Wordpress blog on a new droplet hosting account. Previously the hosting was on Apache, now is on Nginx. I have a problem that now Wordpress pretty links (post-name) are not working and I think the problem comes from the fact that mod)rewrite is not enabled.

Till now I had it in htaccess like

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>

How can I implement this in Nginx and where specifically? I am a complete beginner.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Nginx does not support .htaccess files.

On CentOS Nginx stores virtual host files in /etc/nginx/conf.d. I assume you’re using the default.conf file. So edit this file and find the following:

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

and make the following change:

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
    }

Save the file and reload Nginx:

sudo service nginx reload

In your site’s server block add /index.php?$args; to try_files so that you get something like this:

try_files $uri $uri/ /index.php?$args;

Then go into your Wordpress settings and re-enable pretty permalinks.

I have the following:

try_files $uri $uri/ =404;

Should I just replace the above line with

try_files $uri $uri/ /index.php?$args;

Or should I add it as a new line? Just curious about that before I commit to the change.

Thank you Brian