Nginx服务器下的伪静态


本程序默认支持apache伪静态,后台Generate .htaccess功能可以一键生成apache伪静态规则。

截取代码如下:

Options +SymLinksIfOwnerMatch -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
#If your website is installed in a subfolder, change the line below to reflect the path to the subfolder.
#e.g. for http://www.example.com/subdomain1/subdomain2/ make it RewriteBase /subdomain1/subdomain2

RewriteBase /
#If you wish to use a custom 404 page, place a file named 404.php in your website's root and uncomment the line below.
#If your website is installed in a subfolder, change the line below to reflect the path to the subfolder.
#e.g. for http://www.example.com/subdomain1/subdomain2/ make it ErrorDocument 404 /subdomain1/subdomain2/404.php
#ErrorDocument 404 /404.php

#If your site begins with 'www', uncomment the following two lines
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#DO NOT EDIT BELOW THIS
RewriteRule ^index.php$ "" [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

#tutorial.php
RewriteRule ^tutorial$ "$0/" [R=301,L,QSA]
RewriteRule ^tutorial/$ tutorial.php [L,QSA]
RewriteRule ^tutorial/.*?([^\.\/]*)\.html$ tutorial.php?pname=$1 [L,QSA]
RewriteRule ^tutorial/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ tutorial.php?d=$1$2$3 [L,QSA]
RewriteRule ^tutorial/[^\.]*?([^/\.]*)/$ tutorial.php?fname=$1 [L,QSA]
RewriteRule ^tutorial/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#search.php
RewriteRule ^search$ "$0/" [R=301,L,QSA]
RewriteRule ^search/$ search.php [L,QSA]
RewriteRule ^search/.*?([^\.\/]*)\.html$ search.php?pname=$1 [L,QSA]
RewriteRule ^search/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ search.php?d=$1$2$3 [L,QSA]
RewriteRule ^search/[^\.]*?([^/\.]*)/$ search.php?fname=$1 [L,QSA]
RewriteRule ^search/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#plugins.php
RewriteRule ^plugins$ "$0/" [R=301,L,QSA]
RewriteRule ^plugins/$ plugins.php [L,QSA]
RewriteRule ^plugins/.*?([^\.\/]*)\.html$ plugins.php?pname=$1 [L,QSA]
RewriteRule ^plugins/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ plugins.php?d=$1$2$3 [L,QSA]
RewriteRule ^plugins/[^\.]*?([^/\.]*)/$ plugins.php?fname=$1 [L,QSA]
RewriteRule ^plugins/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#globals.php
RewriteRule ^globals$ "$0/" [R=301,L,QSA]
RewriteRule ^globals/$ globals.php [L,QSA]
RewriteRule ^globals/.*?([^\.\/]*)\.html$ globals.php?pname=$1 [L,QSA]
RewriteRule ^globals/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ globals.php?d=$1$2$3 [L,QSA]
RewriteRule ^globals/[^\.]*?([^/\.]*)/$ globals.php?fname=$1 [L,QSA]
RewriteRule ^globals/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#django.php
RewriteRule ^django$ "$0/" [R=301,L,QSA]
RewriteRule ^django/$ django.php [L,QSA]
RewriteRule ^django/.*?([^\.\/]*)\.html$ django.php?pname=$1 [L,QSA]
RewriteRule ^django/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ django.php?d=$1$2$3 [L,QSA]
RewriteRule ^django/[^\.]*?([^/\.]*)/$ django.php?fname=$1 [L,QSA]
RewriteRule ^django/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#couchcms.php
RewriteRule ^couchcms$ "$0/" [R=301,L,QSA]
RewriteRule ^couchcms/$ couchcms.php [L,QSA]
RewriteRule ^couchcms/.*?([^\.\/]*)\.html$ couchcms.php?pname=$1 [L,QSA]
RewriteRule ^couchcms/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ couchcms.php?d=$1$2$3 [L,QSA]
RewriteRule ^couchcms/[^\.]*?([^/\.]*)/$ couchcms.php?fname=$1 [L,QSA]
RewriteRule ^couchcms/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#component.php
RewriteRule ^component$ "$0/" [R=301,L,QSA]
RewriteRule ^component/$ component.php [L,QSA]
RewriteRule ^component/.*?([^\.\/]*)\.html$ component.php?pname=$1 [L,QSA]
RewriteRule ^component/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ component.php?d=$1$2$3 [L,QSA]
RewriteRule ^component/[^\.]*?([^/\.]*)/$ component.php?fname=$1 [L,QSA]
RewriteRule ^component/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#article.php
RewriteRule ^article$ "$0/" [R=301,L,QSA]
RewriteRule ^article/$ article.php [L,QSA]
RewriteRule ^article/.*?([^\.\/]*)\.html$ article.php?pname=$1 [L,QSA]
RewriteRule ^article/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ article.php?d=$1$2$3 [L,QSA]
RewriteRule ^article/[^\.]*?([^/\.]*)/$ article.php?fname=$1 [L,QSA]
RewriteRule ^article/[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]

#index.php
RewriteRule ^.*?([^\.\/]*)\.html$ ?pname=$1 [L,QSA]
RewriteRule ^([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$ ?d=$1$2$3 [L,QSA]
RewriteRule ^[^\.]*?([^/\.]*)/$ ?fname=$1 [L,QSA]
RewriteRule ^\w[^\.]*?([^/\.]*)$ "$0/" [R=301,L,QSA]
</IfModule>

在Nginx服务器下,变更如下,如果其他栏目请参照此写法。

location / {
 # existing rules
  ###### Couch templates start #####
  if (!-e $request_filename) {
      #solution.php
      rewrite ^/solution$ /solution/ permanent;
      rewrite ^/solution/$ /solution.php last;
      rewrite ^/solution/.*?([^\.\/]*)\.html$ /solution.php?pname=$1 last;
      rewrite "^/solution/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /solution.php?d=$1$2$3 last;
      rewrite ^/solution/[^\.]*?([^/\.]*)/?$ /solution.php?fname=$1 last;

      #product.php
      rewrite ^/product$ /product/ permanent;
      rewrite ^/product/$ /product.php last;
      rewrite ^/product/.*?([^\.\/]*)\.html$ /product.php?pname=$1 last;
      rewrite "^/product/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /product.php?d=$1$2$3 last;
      rewrite ^/product/[^\.]*?([^/\.]*)/?$ /product.php?fname=$1 last;
     
      #news.php
      rewrite ^/news$ /news/ permanent;
      rewrite ^/news/$ /news.php last;
      rewrite ^/news/.*?([^\.\/]*)\.html$ /news.php?pname=$1 last;
      rewrite "^/news/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /news.php?d=$1$2$3 last;
      rewrite ^/news/[^\.]*?([^/\.]*)/?$ /news.php?fname=$1 last;

      #globals.php
      rewrite ^/globals$ /globals/ permanent;
      rewrite ^/globals/$ /globals.php last;
      rewrite ^/globals/.*?([^\.\/]*)\.html$ /globals.php?pname=$1 last;
      rewrite "^/globals/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /globals.php?d=$1$2$3 last;
      rewrite ^/globals/[^\.]*?([^/\.]*)/?$ /globals.php?fname=$1 last;

      #contact.php
      rewrite ^/contact$ /contact/ permanent;
      rewrite ^/contact/$ /contact.php last;
      rewrite ^/contact/.*?([^\.\/]*)\.html$ /contact.php?pname=$1 last;
      rewrite "^/contact/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /contact.php?d=$1$2$3 last;
      rewrite ^/contact/[^\.]*?([^/\.]*)/?$ /contact.php?fname=$1 last;

      #blog.php
      rewrite ^/blog$ /blog/ permanent;
      rewrite ^/blog/$ /blog.php last;
      rewrite ^/blog/.*?([^\.\/]*)\.html$ /blog.php?pname=$1 last;
      rewrite "^/blog/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /blog.php?d=$1$2$3 last;
      rewrite ^/blog/[^\.]*?([^/\.]*)/?$ /blog.php?fname=$1 last;

      #about.php
      rewrite ^/about$ /about/ permanent;
      rewrite ^/about/$ /about.php last;
      rewrite ^/about/.*?([^\.\/]*)\.html$ /about.php?pname=$1 last;
      rewrite "^/about/([1-2]\d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /about.php?d=$1$2$3 last;
      rewrite ^/about/[^\.]*?([^/\.]*)/?$ /about.php?fname=$1 last;

      #index.php
      rewrite ^/.*?([^\.\/]*).html$ /?pname=$1 last;
      rewrite "^/([1-2]d{3})/(?:(0[1-9]|1[0-2])/(?:(0[1-9]|1[0-9]|2[0-9]|3[0-1])/)?)?$" /?d=$1$2$3 last;
      rewrite ^/[^\.]*?([^/\.]*)/$ /?fname=$1 last;
  }
  ####End Couch templates ######
}