Changing permalinks gives 404 errors on nginx

If you found this post helpful, kindly share it and show your support :)

Scenario :-

  • Changed permalinks from the default to /%postname%/, and now the links within WordPress’s Admin panel give me 404 errors – Not WordPress 404 pages, nginx 404 pages.
  • Tried creating a blank .htaccess file in the wordpress folder, giving it 666 permissions, changing the user and group to www-data and then changing the permalinks- that didn’t work.
  • Then changed it to this before changing the permalinks:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
  • When that didn’t work, changed RewriteBase to /wordpress/ before changing permalinks again – still nothing.
  • Also gone into the site’s .conf file and changed try_files $uri $uri/ /index.php; to the following, restarting nginx and php5-fpm each time;
try_files $uri $uri/ /index.php?$query_string;

try_files $uri $uri/ /index.php?q=$request_uri;

try_files $uri $uri/ /index.php?$args;
  • Running a server with nginx.

Solution :-

Those are Apache .htaccess rewrite rules, but it stated that domain is hosted on an Nginx server. Nginx does not use an .htaccess-like directory level file, much less does it use the .htaccess file itself. So, we need to edit the site’s nginx configuration itself.

 sudo vi /etc/nginx/site-enabled/domain.conf
  • Edit/add/append the following location block within the server block:

Sample configuration file is as follows :-

# WordPress single blog rules.
# Designed to be included in any server {} block.

# The rest of your server block

# This order might seem weird - this is attempted to match last if rules below fail.
location / {
try_files $uri $uri/ /index.php?$args;

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
  • If you are using wordpress multisite with custom permalink setting: /%category%/%postname%/

On server

location / {
try_files $uri $uri/ /index.php?q=$uri$args;
  • If your root wordpress is not the webroot but
location /wordpress/ {
try_files $uri $uri/ /wordpress/index.php?q=$uri$args;
  • If you are using old wordpress with blogs.dir,
add: location ^~ /blogs.dir { internal; alias /var/www/wordpress/wp-content/blogs.dir; access_log off; log_not_found off; expires max; }

Check the nginx configuration:

sudo nginx -t

Reload nginx:

sudo service nginx reload

Also try change permalink settings.


Mohammed Noufal

I'm Mohammed Noufal, working as Server Admin for the last 10 years.  In my day-to-day life, l had to face different problems related to Web-hosting. On my website, you can find solutions for different Web-hosting-related problems. Sometimes, I spent hours searching/googling to find a solution. This inspired me to start a website that gives solutions to different Webhosting problems. This website includes basic Linux and windows commands, and different control panels like cPanel, Plesk, DirectAdmin, Webmin & so on. You can find solutions and or suggestions for different Web-hosting related problems from here. Hence I would like to say Errorlogz is your server protector.  I will be glad if Logdetect can help any one of the Server admins to find a solution to his problem.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *