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!
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 ...
}
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:
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.
The server block is defined by
server {
...
}
By default it resides within the configuration file in /etc/nginx/sites-available
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.