WordPress permalinks
To make WordPress permalinks work you have to add a specific redirect command. This command works also to varius CMS program as well.
Find the «location / {» block inside the configuration file default.conf and add the following line to it.
try_files $uri $uri/ /index.php?q=$uri&$args;
With this command nginx checks for the existence of a file at the URL ($uri), then for a directory ($uri/). If doesn’t find them it performs an internal redirect to /index.php passing the query string arguments as parameters.
If your WordPress site isn’t on the root folder but inside a subfolder ( lets say /blog) you have to add the /blog before the /index argument:
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
In case those redirects don’t work (most likely Drupal 7 or newer) remove q=$uri. So the commands will be:
try_files $uri $uri/ /index.php?$args;
or for subfolder /blog:
try_files $uri $uri/ /blog/index.php?$args;
301 Redirects
from www to non-www sites or from non-www to www sites
The domain name for your sites may use www infront of it (www.example.org) or not (example.org). You can let the visitors choose the url they want to visit or you can redirect them to the url you want your site to appear. The 301 Redirects are usefull for this job. You can redirect from a www to a not-www site (www.example.org to example.org) or from a non-www to a www site (example.org to www.example.org).
You can do these redirects with two ways. The first is by using the rewrite directive and the second using the return 301 directive. The second way is the recommended way. Let’s see both of them:
From www to non-www site:
Recommended way:
server {
server_name www.domainname.com;
return 301 $scheme://domainname.com$request_uri;
}
Traditional way:
server {
server_name www.domainname.com;
rewrite ^/(.*)$ http://domain.com/$1 permanent;
}
From non-www to www site:
Recommended way:
server {
server_name domainname.com;
return 301 $scheme://www.domainname.com$request_uri;
}
Traditional way:
server {
server_name domainname.com;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}
Remember that we changing the default configuration file in order to use it with our future site. So we want it flexible. We will include both redirects and we comment all the lines. When and if we want to use a 301 redirect we uncomment the proper lines.
So, open the default configuration file and at the top of it write the generic code:
#server {
#non-www to www redirect 301
#server_name domain.com;
#return 301 $scheme://www.domain.com$request_uri;
#www to non-www redirect 301
#server_name www.domain.com;
#return 301 $scheme://domain.com$request_uri;
#}
We comment all the lines so there is no redirect there. If you want to do a redirect, lets say from www to non-www make this look like:
server {
#non-www to www redirect 301
#server_name domain.com;
#return 301 $scheme://www.domain.com$request_uri;
#www to non-www redirect 301
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
Off course you replace the domain.com and www.domain.com with your domain name (thiella.net and www.thiella.net for my site).
Fix for Sitemap for WordPress plugin Yoast’s WordPress-SEO
If you are going to use the WordPress plugin Yoast’s WordPress-SEO (one of the best SEO plugins btw) you have to add this code to the block «location / {»
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
Creating the first virtual site
This configuration file displays an error page when someone tries to see a domain that leeds to your server IP address but you don’t have set it up yet or you have set wrong settings. We will talk about this later.
alert("Test.");
Pingback: URL