天天看點

為wordpress配置apache url重定向/apache url rewrite for wordpress

文章來源:http://degula.com/wordpress/200903166/wordpress-apache-rewrite.html#more-166

測試環境: windows xp/apache 2.2.11/wordpress2.7

看了很多文章都說靜态url便于搜尋引擎的收錄,昨天晚上就将degula

的連結全都改為靜态連結了,我的連結格式是:/%category%/%year%%monthnum%%post_id%/%postname%.html,效果還不錯,現在是萬事具備,隻等收錄了:)

今天小d

在自己的winxp上配置wordpress永久連結時發現頁面找不到,報404錯誤.想了一下應該是apache url rewrite預設沒有開啟的原因.找到原因就開始動手了.這裡把主要步驟和遇到的一些疑點寫出來,希望能對大家有所幫助.

首先需要在配置httpd.conf以啟用url rewrite功能:

将#loadmodule rewrite_module modules/mod_rewrite.so前面的登出去掉然後重新開機apache,在指令行使用httpd -m檢視目前子產品情況.如有rewrite_module(shared)說明子產品已經成功加載.

注:網上許多文章提到了httpd.conf檔案中需要有這一行:addmodule mod_rewrite.c,其實在這裡是不需要的,因為apache已經把mod_rewrite.c編譯進去了.下面是關于mod_rewrite.c的一點說明:

the addmodule and clearmodulelist directives no longer exist. these directives were used to ensure that modules could be enabled in the correct order. the new apache 2.0 api allows modules to explicitly specify their ordering, eliminating the need for these directives. as long as you have mod_rewrite.c ompiled into apache you should be ready to roll. use the loadmodule if you enabled the module as a dso .

到這裡apache url rewrite的子產品已經加載了.要想使它生效還需要配置.htaccess檔案.

配置目錄的allowoverwrite屬性:

在apache 2.x 中,我們會看到 documentroot設定的一行。這行就是存放網頁程式的地方。比如存放在 c:/www 目錄中,那麼我們就要設定 documentroot為如下的:

<code>documentroot "c:/www"</code>

</blockquote>

然後我們再還要對 documentroot做針對性的行為設定。在一般的情況下,httpd.conf 會給一個預設的。如果你要改 documentroot的路徑,同時也要改針對documentroot的directory的設定,也就是

<code>&lt;directory "documentroot中設定的路徑"&gt;</code>

比如我們把documentroot的路徑改為了 “c:/www”,那我們也要把 documentroot做針對性的行為設定也要改成這個路徑.

注意不要

修改一下default的配置

&lt;directory /&gt;     options followsymlinks        allowoverride none     order deny,allow     deny from all &lt;/directory&gt;

而是修改這裡:

&lt;directory “c:/www”&gt;     #     # possible values for the options directive are “none”, “all”,     # or any combination of:     #   indexes includes followsymlinks symlinksifownermatch execcgi multiviews     # note that “multiviews” must be named *explicitly* — “options all”     # doesn’t give it to you.     # the options directive is both complicated and important.  please see     # http://httpd.apache.org/docs/2.2/mod/core.html#options     # for more information.     options indexes followsymlinks     # allowoverride controls what directives may be placed in .htaccess files.     # it can be “all”, “none”, or any combination of the keywords:     #   options fileinfo authconfig limit     allowoverride all     # controls who can get stuff from this server.     order allow,deny     allow from all

把allowoverride 的參數設定為all,表示整台伺服器上的,都支援url規則重寫。apache

伺服器要讀每個網站下的家目錄下的 .htaccess

檔案。如果沒有這個檔案,或者這個文檔沒有定義任何關于url重寫的規則,則不會有任何效果。在一般的情況下,成熟的web

伺服器應用套件,都支援url重寫的,比如drupal和joomla 。當我們用這些程式時,會發現在安裝包中有

.htaccess中有這個檔案。我們把apache配置好後,隻是需要在這些程式的背景打開此功能就行了.

下面是wordpress安裝包中的.htaccess檔案

# begin wordpress &lt;ifmodule mod_rewrite.c&gt; rewriteengine on rewritebase /wordpress/ rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule . /wordpress/index.php [l] &lt;/ifmodule&gt; # end wordpress

到這裡應該一切ok了吧,通路剛才404的網址,果然可以了.wper們,還等什麼,快把你的blog連結也和諧了吧.

apache文檔:

中文文檔:http://www.phpx.com/man/apache-2/mod/mod_rewrite.html 中文文檔:http://www.kreny.com/docs/apache2.0/misc/rewriteguide.html 英文文檔:http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

參考文獻:

&lt;apache url rewrite 重寫入門&gt; &lt;apache url rewrite(mod_rewrite)的使用&gt; &lt;apache 2.x 伺服器中的url重寫的配置和應用&gt;