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.
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!
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.
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 thelocation
block and therewrite
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 therewrite
directive inside your server block. For example, to rewrite requests from one URL to another, you can use:In the above example, requests to
/old-url
will be permanently redirected to/new-url
.Test Configuration and Reload Nginx:
Before applying the changes, test the Nginx configuration to ensure there are no syntax errors:
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:
and make the following change:
Save the file and reload Nginx:
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.