{"id":150,"date":"2016-11-20T20:01:02","date_gmt":"2016-11-20T12:01:02","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=150"},"modified":"2016-11-20T20:01:02","modified_gmt":"2016-11-20T12:01:02","slug":"nginx%e4%b8%8bwordpress%e7%9a%84rewrite","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=150","title":{"rendered":"Nginx\u4e0bWordPress\u7684Rewrite"},"content":{"rendered":"<p>\u6700\u8fd1\u63a5\u89e6<code>WP Super Cache<\/code>\uff0c\u8be5\u63d2\u4ef6\u8981\u6c42\u56fa\u5b9a\u94fe\u63a5\u5fc5\u987b\u662f\u91cd\u5199\u7684\uff0c\u6545\u7528\u5230Rewrite\u3002<\/p>\n<p>\u6211\u7684\u662f\u8fd9\u6837\u914d\u7f6e\u7684\uff1a<\/p>\n<p><code>\/usr\/local\/nginx\/conf\/rewrite\/wordpress.conf<\/code><\/p>\n<pre><code class=\"hljs perl\">location \/ {\r\n    <span class=\"hljs-keyword\">if<\/span> (-f $request_filename\/<span class=\"hljs-keyword\">index<\/span>.html){\r\n        rewrite (.*) $1\/<span class=\"hljs-keyword\">index<\/span>.html <span class=\"hljs-keyword\">break<\/span>;\r\n    }\r\n    <span class=\"hljs-keyword\">if<\/span> (-f $request_filename\/<span class=\"hljs-keyword\">index<\/span>.php){\r\n        rewrite (.*) $1\/<span class=\"hljs-keyword\">index<\/span>.php;\r\n    }\r\n    <span class=\"hljs-keyword\">if<\/span> (!-f $request_filename){\r\n        rewrite (.*) \/<span class=\"hljs-keyword\">index<\/span>.php;\r\n    }\r\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728\u865a\u62df\u4e3b\u673a\u6587\u4ef6\u91cc\u6dfb\u52a0<\/p>\n<pre><code class=\"hljs dts\">include <span class=\"hljs-meta-keyword\">\/usr\/<\/span>local<span class=\"hljs-meta-keyword\">\/nginx\/<\/span>conf<span class=\"hljs-meta-keyword\">\/rewrite\/<\/span>wordpress.conf;<\/code><\/pre>\n<p>\u5373\u53ef\u3002<\/p>\n<p>\u5b8c\u6574\u7684vhost\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"hljs nginx\"><span class=\"hljs-section\">server<\/span> {\r\n    <span class=\"hljs-attribute\">listen<\/span>       <span class=\"hljs-number\">80<\/span>;\r\n    <span class=\"hljs-attribute\">server_name<\/span>  me.52fhy.com;\r\n    <span class=\"hljs-attribute\">index<\/span> index.php index.html index.htm;\r\n    <span class=\"hljs-attribute\">root<\/span> \/52fhy.com\/wordpress\/;\r\n    \r\n    <span class=\"hljs-attribute\">location<\/span> \/ {\r\n        <span class=\"hljs-attribute\">if<\/span> (-f <span class=\"hljs-variable\">$request_filename<\/span>\/index.html){\r\n            <span class=\"hljs-attribute\">rewrite<\/span> (.*) <span class=\"hljs-variable\">$1<\/span>\/index.html <span class=\"hljs-literal\">break<\/span>;\r\n        }\r\n        <span class=\"hljs-attribute\">if<\/span> (-f <span class=\"hljs-variable\">$request_filename<\/span>\/index.php){\r\n            <span class=\"hljs-attribute\">rewrite<\/span> (.*) <span class=\"hljs-variable\">$1<\/span>\/index.php;\r\n        }\r\n        <span class=\"hljs-attribute\">if<\/span> (!-f <span class=\"hljs-variable\">$request_filename<\/span>){\r\n            <span class=\"hljs-attribute\">rewrite<\/span> (.*) \/index.php;\r\n        }\r\n    }\r\n    \r\n    <span class=\"hljs-attribute\">location<\/span> <span class=\"hljs-regexp\">~ .*\\.(php|php5)?$<\/span>\r\n    {\r\n            <span class=\"hljs-comment\">#fastcgi_pass  unix:\/tmp\/php-cgi.sock;<\/span>\r\n            <span class=\"hljs-attribute\">fastcgi_pass<\/span>  <span class=\"hljs-number\">127.0.0.1:9000<\/span>;\r\n            <span class=\"hljs-attribute\">fastcgi_index<\/span> index.php;\r\n            <span class=\"hljs-attribute\">include<\/span> fastcgi.conf;\r\n    }\r\n    \r\n    <span class=\"hljs-attribute\">location<\/span> <span class=\"hljs-regexp\">~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$<\/span>\r\n    {\r\n        <span class=\"hljs-attribute\">expires<\/span> <span class=\"hljs-number\">30d<\/span>;\r\n    }\r\n    \r\n    <span class=\"hljs-attribute\">location<\/span> <span class=\"hljs-regexp\">~ .*\\.(js|css)?$<\/span>\r\n    {\r\n        <span class=\"hljs-attribute\">expires<\/span> <span class=\"hljs-number\">1h<\/span>;\r\n    }\r\n\r\n    <span class=\"hljs-attribute\">access_log<\/span>  \/www\/log\/me.52fhy.com.log;\r\n}<\/code><\/pre>\n<ul>\n<li><strong>2015-12-18 11:42:24\u66f4\u65b0\uff1a<\/strong><br \/>\n\u4ee5\u4e0arewrite\u65b9\u6cd5\u5bfc\u81f4\u540e\u53f0\u5f88\u591a\u83dc\u5355\u65e0\u6cd5\u8bbf\u95ee\uff0c\u73b0\u66f4\u65b0\u3002\u539f\u56e0\u662f\uff1a<\/p>\n<pre><code class=\"hljs perl\">location \/ {\r\n<span class=\"hljs-comment\">#1<\/span>\r\n<span class=\"hljs-keyword\">if<\/span> (-f $request_filename\/<span class=\"hljs-keyword\">index<\/span>.html){\r\n    rewrite (.*) $1\/<span class=\"hljs-keyword\">index<\/span>.html <span class=\"hljs-keyword\">break<\/span>;\r\n}\r\n\r\n<span class=\"hljs-comment\">#2<\/span>\r\n<span class=\"hljs-keyword\">if<\/span> (-f $request_filename\/<span class=\"hljs-keyword\">index<\/span>.php){\r\n    rewrite (.*) $1\/<span class=\"hljs-keyword\">index<\/span>.php;\r\n}\r\n<span class=\"hljs-comment\">#3<\/span>\r\n<span class=\"hljs-keyword\">if<\/span> (!-f $request_filename){\r\n    rewrite (.*) \/<span class=\"hljs-keyword\">index<\/span>.php;\r\n}\r\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u540e\u53f0<code>http:\/\/me.52fhy.com\/wp-admin\/options-writing.php<\/code>\u8fd9\u79cd\u94fe\u63a5\u5c06\u76f4\u63a5\u5339\u914d<code>#3<\/code>\uff0c\u5b9e\u9645\u4e0a\u8fd9\u65f6\u5019\u4e0d\u9700\u8981\u4efb\u4f55\u5339\u914d\u3002\u6545\u53ef\u5728<code>#2<\/code>\u524d\u6dfb\u52a0<\/p>\n<pre><code class=\"hljs bash\"><span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-_\">-f<\/span> <span class=\"hljs-variable\">$request_filename<\/span>){\r\n<span class=\"hljs-built_in\">break<\/span>;\r\n}<\/code><\/pre>\n<p>\u6216\u8005\u5168\u90e8\u66f4\u65b0\u66f4\u65b0\u4e3a\uff1a<\/p>\n<pre><code class=\"hljs perl\">location \/ {\r\n<span class=\"hljs-keyword\">index<\/span> <span class=\"hljs-keyword\">index<\/span>.html <span class=\"hljs-keyword\">index<\/span>.php;\r\n<span class=\"hljs-keyword\">if<\/span> (-f $request_filename) {\r\n          <span class=\"hljs-keyword\">break<\/span>;\r\n}\r\n<span class=\"hljs-keyword\">if<\/span> (!-e $request_filename) {\r\n          rewrite . \/<span class=\"hljs-keyword\">index<\/span>.php  <span class=\"hljs-keyword\">last<\/span>;\r\n}\r\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u53e6\u5916\uff0c\u5728Apache\u4e0b\uff0c\u5229\u7528mod_rewrite\u6765\u5b9e\u73b0URL\u7684\u9759\u6001\u5316\u3002<\/p>\n<p><code>.htaccess<\/code>\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"hljs apache\"><span class=\"hljs-comment\"># BEGIN WordPress<\/span>\r\n<span class=\"hljs-attribute\"><span class=\"hljs-nomarkup\">RewriteEngine<\/span><\/span> <span class=\"hljs-literal\">On<\/span>\r\n<span class=\"hljs-attribute\">RewriteBase<\/span> \/\r\n<span class=\"hljs-attribute\"><span class=\"hljs-nomarkup\">RewriteCond<\/span><\/span> <span class=\"hljs-variable\">%{REQUEST_FILENAME}<\/span> !-f\r\n<span class=\"hljs-attribute\"><span class=\"hljs-nomarkup\">RewriteCond<\/span><\/span> <span class=\"hljs-variable\">%{REQUEST_FILENAME}<\/span> !-d\r\n<span class=\"hljs-attribute\"><span class=\"hljs-nomarkup\">RewriteRule<\/span><\/span> . \/index.php<span class=\"hljs-meta\"> [L]<\/span>\r\n<span class=\"hljs-comment\"># END WordPress<\/span><\/code><\/pre>\n<p>\u539f\u6587\uff1ahttps:\/\/yq.aliyun.com\/articles\/33423<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u63a5\u89e6WP Super Cache\uff0c\u8be5\u63d2\u4ef6\u8981\u6c42\u56fa\u5b9a\u94fe\u63a5\u5fc5\u987b\u662f\u91cd\u5199\u7684\uff0c\u6545\u7528\u5230Re&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=150\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=150"}],"version-history":[{"count":1,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}