location / {}
http://site.com/notes/343
http://site.com/note.php?id=343
rewrite ^/notes/(.*)$ /notes.php?id=$1 last;
http://site.com/users/BlackBenzKid
http://site.com/user.php?id=1
rewrite ^/users/(.*)$ /user.php?username=$1 last;
http://site.com/top
http://site.com/top.php
rewrite ^/top?$ /top.php last;
Complex and further
http://site.com/users/BlackBenzKid/gallery
http://site.com/user.php?username=1&page=gallery
rewrite ^/users/(.*)$/gallery /user.php?username=$1&page=gallery last;
##############################################################
##############################################################
If you are using WordPress with Nginx, you will need to setup Nginx specific rewrite rules. This guide will give you the rules you need to get pretty URLs working for your WordPress install.
The two projects I use the most for my web development projects are WordPress andCodeIgniter, both of which use rewrite rules for their pretty URLs. My last post coveredCodeIgniter Nginx Rewrite Rules and in this post I will go over WordPress rewrite rules for Nginx.
WordPress Nginx Config
Here is the entire config file that I use. Remember that some of these rules are commented out, you will have to explicitly enabled them by removing the comments (removing the “#” from in front of the rule).
server
{
server_name .example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/example.com/html;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
#if ($host !~* ^(www|subdomain))
#{
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
#}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
One Important Note
The PHP DOCUMENT_ROOT environment variable is derived from the root parameter and setting root on the server level vs within a location block is important.
Nginx Rewrite Rule Breakdown
The first set of rules allow you to enforce “www” or enforce NO “www”, you will have to pick which you prefer and enable one or the other. Additionally, for those who may have subdomains, adding the subdomains to the list (if enforcing www) will safely exclude those subdomains from being redirected.
# enforce www (exclude certain subdomains)
#if ($host !~* ^(www|subdomain))
#{
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
#}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
The final rule is the catch all to route all requests to the bootstrap file.
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
Apply These Final Touches to Clean Up the URL
Remember, once you have your WordPress Nginx config setup, you will want to give your website some nice URLs.
http://www.farinspace.com/wordpress-nginx-rewrite-rules/
http://www.farinspace.com/wordpress-nginx-rewrite-rules/
ไม่มีความคิดเห็น:
แสดงความคิดเห็น