URL重寫是截取傳入Web請求並自動將請求重定向到其餘URL的過程。
好比:php
hostname/list_1,服務器自動將其定向爲http://hostname/list.php?id=1
該技術經常使用於引擎優化(SEO)html
URL重寫的好處web
僞靜態也是重寫的一種實現。 URL重寫有兩種實現正則表達式
實現list.php?Mode=A重寫爲僞靜態list-A.html(假設工做目錄爲E:/dev/www/php/new)apache
#LoadModule rewrite_module modules/mod_rewrite.so
把前面#去掉。沒有則添加,但必選獨佔一行,使apache支持 mod_rewrite 模塊瀏覽器
<Directory "D:/ApacheServer/web"> # # 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 None # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory>
把AllowOverride None 換成 AllowOverride All 使apache支持 .htaccess 文件服務器
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule index.html index.php RewriteRule list-([A-Z+])\.html $list.php?mode=$1 [NC] </IfModule>
註釋: RewriteEngine 爲重寫引擎開關,on爲開啓,off爲關閉。ide
RewriteRule 是路由轉向規則,$ 以前路徑爲瀏覽器中要輸入路徑,這裏能夠用正則表達式表達。$+空格 後路徑爲後臺實際轉向路徑, 轉向後臺實際路徑時能夠傳參數,例子裏的後臺頁面能夠用$_GET['p'] $_GET['action'] $_GET['id'] 來接收 $1 表明瀏覽器路徑中輸入的第一個正則表達式的值,以此類推,$2表明第二個正則表達式的值 RewriteRule 路由轉向規則里正則表達式用括號 () 括起來優化