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.

KFSys
Site Moderator
Site Moderator badge
January 31, 2024
Pinned Answer

In Nginx, there is no direct equivalent of Apache’s mod_rewrite module because Nginx uses a different approach for handling URL rewriting and redirection. In Nginx, you typically use the location block and the rewrite directive to achieve URL rewriting and redirection tasks. Here’s how to enable and use URL rewriting in Nginx:

Configure URL Rewriting:

To configure URL rewriting in Nginx, use the location block with the rewrite directive inside your server block. For example, to rewrite requests from one URL to another, you can use:

server {
    listen 80;
    server_name your_domain.com;

    location /old-url {
        rewrite ^/old-url(.*)$ /new-url$1 permanent;
    }

    # ... other server configuration ...
}
  1. In the above example, requests to /old-url will be permanently redirected to /new-url.

  2. Test Configuration and Reload Nginx:

    Before applying the changes, test the Nginx configuration to ensure there are no syntax errors:

sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Now, Nginx should be configured to handle URL rewriting based on the rules you've defined.

Remember to adapt the configuration to your specific needs, and consult the Nginx documentation for more advanced URL rewriting and redirection scenarios. Nginx’s approach to rewriting is more flexible and powerful than Apache’s mod_rewrite, but it may require a different mindset and syntax.

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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.