apache php 開啟偽靜態


打開apache的配置文件httpd.conf
找到
#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 文件

重啟apache服務器

在要啟用偽靜態的 PHP 項目根目錄下建立 .htaccess 文件

在 .htaccess 文件中輸入內容

<IfModule mod_rewrite.c>
    Options +FollowSymlinks -Multiviews
    RewriteEngine on
    #本地測試的例子
    RewriteRule index.html$ index.php
    RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
    RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
    
    
    #再附幾個常用的規則內容
    #訪問index.html導向訪問index.php?page=index
    RewriteRule index.html$ index.php?page=index [L]


    #禁止訪問git文件夾
    RewriteRule ^.git - [F,L]


    #301重定向,自動在網址后加反斜杠 / ,文件路徑除外(會先訪問該文件,找不到則在文件路徑后也加 /)
    #RewriteBase / 是設置了重寫的基准為該域名的根目錄,寫了這個的話,RewriteRule . index.php 就可以了,但是沒寫的話,就要多一個斜杠了RewriteRule . /index.php
    RewriteBase /
    #如果不是文件,才會執行下一條RewriteRule。
    RewriteCond %{REQUEST_FILENAME} !-f
    #如果不是目錄,才會執行下一條RewriteRule
    #RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ $1/ [L,R=301]
    
    #當訪問products/mobile-phones/gdshdfh/或者products/tablets/gdshdfh/或者products/wear/gdshdfh/ 等的時候都指向同一路徑 products/overview/ename/$2/,gdshdfh這里為亂想,匹配正則2的公式[a-zA-Z0-9-]{1,}即可
    RewriteRule products/(mobile-phones|tablets|wear|home-internet-media|accessories)/([a-zA-Z0-9-]{1,})/$ products/overview/ename/$2/ [QSA,PT,L]
    #偽靜態也有緩存,修改規則后要清緩存
</IfModule>

注釋:
RewriteEngine   為重寫引擎開關,on為開啟,off為關閉。

RewriteRule     是路由轉向規則,$ 之前路徑為瀏覽器中要輸入路徑,這里可以用正則表達式表達。$+空格 后路徑為后台實際轉向路徑,
轉向后台實際路徑時可以傳參數,例子里的后台頁面可以用$_GET['p']   $_GET['action']  $_GET['id'] 來接收
$1 代表瀏覽器路徑中輸入的第一個正則表達式的值,以此類推,$2代表第二個正則表達式的值
RewriteRule 路由轉向規則里正則表達式用括號 () 括起來

RewriteRule 后面中括號里的大寫字符注釋
NC  : no case,就是說不區分大小寫
R   : redirect,重定向
F   : forbidden,禁止訪問,強制當前URL為被禁止的,即,立即反饋一個HTTP響應代碼403(被禁止的)。 使用這個標記,可以鏈接若干RewriteConds以有條件地阻塞某些URL
L   : last,表示已經是最后一條規則,立即停止重寫操作,並不再應用其他重寫規則。
QSA : qsappend,此標記強制重寫引擎在已有的替換串中追加一個請求串,而不是簡單的替換。 如果需要通過重寫規則在請求串中增加信息,就可以使用這個標記。
PT  : passthrough,此標記強制重寫引擎將內部結構request_rec中的uri字段設置為 filename字段的值,它只是一個小修改,使之能對來自其他URI到文件名翻譯器的 Alias,ScriptAlias, Redirect 等指令的輸出進行后續處理。舉一個能說明其含義的例子: 如果要通過mod_rewrite的重寫引擎重寫/abc為/def, 然后通過mod_alias使/def轉變為/ghi,
            可以這樣: RewriteRule ^/abc(.*) /def$1 [PT]
      Alias /def /ghi
            如果省略了PT標記,雖然mod_rewrite運作正常, 即, 作為一個使用API的URI到文件名翻譯器, 它可以重寫uri=/abc/…為filename=/def/…, 但是,后續的mod_alias在試圖作URI到文件名的翻譯時,則會失效。
            注意: 如果需要混合使用不同的包含URI到文件名翻譯器的模塊時, 就必須使用這個標記。混合使用mod_alias和mod_rewrite就是個典型的例子。
      For Apache hackers
            如果當前Apache API除了URI到文件名hook之外,還有一個文件名到文件名的hook, 就不需要這個標記了! 但是,如果沒有這樣一個hook,則此標記是唯一的解決方案。 Apache Group討論過這個問題,並在Apache 2.0 版本中會增加這樣一個hook。

+FollowSymlinks : 打開FollowSymlinks項,某些服務器配置中,mod_rewrite要求有followsymlinks,否則會顯示500內部服務器錯誤,如果沒有指定FollowSymLinks的選項(即Options FollowSymLinks),或者指定了 SymLinksIfOwnerMatch選項,Apache將不得不調用額外的系統函數來檢查符號鏈接。每次針對文件名的請求都將觸發一次檢查

-Multiviews     : 關閉Multiviews,MultiViews的效果是:如果服務器收到對/some/dir/foo的請求,而/some/dir/foo並不存在,但是如果/some/dir 啟用了MultiViews ,則服務器會查找這個目錄下所有的foo.* 文件,並有效地偽造一個說明這些foo.* 文件的類型表,分配給他們相同的媒體類型及內容編碼,並選擇其中最合適的匹配返回給客戶



例子所在項目為test
在項目下 index.php 頁面內寫入內容
<?php
if ($_GET ['p']) {
    echo "p : " . $_GET ['p'];
}

if ($_GET ['action']) {
    echo "action : " . $_GET ['action'];
}

if ($_GET ['id']) {
    echo "id : " . $_GET ['id'];
}
?>

在瀏覽器中輸入
http://localhost/test/index.html
http://localhost/test/index-99.html
http://localhost/test/page-18.html

都會轉向 http://localhost/test/index.php 頁面
並且依次
http://localhost/test/index.html     頁面什么都不顯示
http://localhost/test/index-99.html  頁面顯示 p : 99
http://localhost/test/page-18.html   頁面顯示 action : pageid : 18

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM