apache php 開啟偽靜態

2021-07-28 09:30:05 字數 2103 閱讀 9724

開啟apache的配置檔案httpd.conf

找到#loadmodule rewrite_module modules/mod_rewrite.so

把前面#去掉。沒有則新增,但必選獨佔一行,使apache支援 mod_rewrite 模組找到#

# 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

# # 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

把allowoverride none 換成 allowoverride all 使apache支援 .htaccess 檔案

重啟apache伺服器

在要啟用偽靜態的 php 專案根目錄下建立 .htaccess 檔案

在 .htaccess 檔案中輸入內容

rewriteengine on

rewriterule index.html$ index.php

rewriterule index-([1-9]+[0-9]*).html$ index.php?p=$1

rewriterule ([a-z])-([0-9]).html$ index.php?action=$1&id=$2

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

rewriterule     是路由轉向規則,$ 之前路徑為瀏覽器中要輸入路徑,這裡可以用正規表示式表達。$+空格 後路徑為後台實際轉向路徑,

轉向後台實際路徑時可以傳引數,例子裡的後台頁面可以用$_get['p']   $_get['action']  $_get['id'] 來接收

$1 代表瀏覽器路徑中輸入的第乙個正規表示式的值,以此類推,$2代表第二個正規表示式的值

rewriterule 路由轉向規則裡正規表示式用括號 () 括起來

例子所在專案為test

在專案下 index.php 頁面內寫入內容

<?php

if ($_get ['p'])

if ($_get ['action'])

if ($_get ['id'])

?>

在瀏覽器中輸入

都會轉向 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

Apache PHP開啟偽靜態

開啟apache的配置檔案httpd.conf 第一步 找到 loadmodule rewrite module modules mod rewrite.so把前面 去掉。沒有則新增,但必選獨佔一行,使apache支援 mod rewrite 模組 第二步 找到 possible values fo...

Apache開啟偽靜態

開啟apache的配置檔案httpd.conf 找到 loadmodule rewrite module modules mod rewrite.so 把前面 去掉。沒有則新增,但必選獨佔一行,使apache支援 mod rewrite 模組找到 possible values for the op...

Apache開啟偽靜態

apache開啟偽靜態 loadmodule rewrite module modules mod rewrite.so 把前面 去掉。沒有則新增,但必選獨佔一行,使apache支援 mod rewrite 模組找到 possible values for the options directive...