為wordpress配置apache url重定向/apache url rewrite for wordpress
文章來源:https://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為如下的:
DocumentRoot "c:/www"
然後我們再還要對 DocumentRoot做針對性的行為設置。在一般的情況下,httpd.conf 會給一個默認的。如果你要改 DocumentRoot的路徑,同時也要改針對DocumentRoot的Directory的設置,也就是
<Directory "DocumentRoot中設置的路徑">
比如我們把DocumentRoot的路徑改為了 “c:/www”,那我們也要把 DocumentRoot做針對性的行為設置也要改成這個路徑.
注意不要 修改一下default的配置
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
而是修改這裏:
<Directory “c:/www”>
#
# 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
# https://httpd.apache.org/docs/2.2/mod/core.html
# 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</Directory>
把AllowOverride 的參數設置為ALL,表示整台服務器上的,都支持URL規則重寫。Apache 服務器要讀每個網站下的家目錄下的 .htaccess 文件。如果沒有這個文件,或者這個文檔沒有定義任何關於URL重寫的規則,則不會有任何效果。在一般的情況下,成熟的Web 服務器應用套件,都支持URL重寫的,比如drupal和joomla 。當我們用這些程序時,會發現在安裝包中有 .htaccess中有這個文件。我們把Apache配置好後,隻是需要在這些程序的後台打開此功能就行了.
下麵是WordPress安裝包中的.htaccess文件
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule># END WordPress
到這裏應該一切OK了吧,訪問剛才404的網址,果然可以了.WPer們,還等什麼,快把你的blog鏈接也和諧了吧.
Apache文檔:
中文文檔:https://www.phpx.com/man/Apache-2/mod/mod_rewrite.html
中文文檔:https://www.kreny.com/docs/apache2.0/misc/rewriteguide.html
英文文檔:https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
參考文獻:
<apache url rewrite 重寫入門>
<Apache Url Rewrite(mod_rewrite)的使用>
<Apache 2.x 服務器中的URL重寫的配置和應用>
最後更新:2017-04-02 04:01:44