PHP偽靜態入門基於教程

來源:互聯網
上載者:User

 

Apache偽靜態html(URL Rewrite)設定法

phpma一 開啟 Apache 的設定檔 httpd.conf 。

phpma二 將#LoadModule rewrite_module modules/mod_rewrite前面的#去掉


1、apache

現在我們可以實現偽靜態頁面了,寫下一下的規則:

 代碼如下 複製代碼

<ifmodule mod_rewrite.c> #liunx下需要寫

rewriteengine on

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

</ifmodule>


([a-za-z]{1,})-([0-9]{1,}).html$是規則,index.php?action=$1&id=$2是要替換的格式,$1代表第一個括弧匹配的值,$2代表第二個

 

然後重啟動apache

2、.htaccess 在目錄裡建立個檔案命名為 .htaccess

、.htaccess檔案的書寫

在網站根目錄下建立一個檔案,名稱是.htaccess,書寫方式:

 代碼如下 複製代碼

RewriteEngine on  #開啟重寫
RewriteRule ^/$ index.php  #表示用“/”就可以訪問index.php
RewriteRule ^about_(d*)/$ about/about.php?id=$1  #表示可以使用about_22/訪問到about/about.php?id=22頁面。注意$前面的“/”
RewriteRule ^about_(d*).html$ about/about.php?id=$1  #表示可以使用about_22.html訪問到about/about.php?id=22頁面RewriteRule ^news_(d*)_(d*).html$ news/news.php?id=$1&page=$2  #表示可以使用news_11_2.html訪問到news/news.php?id=11&page=2頁面。$1表示第一個參數,$2表示第二個參數


從上面可以看出,如果我們有下面的連結

<a href="/about_22/">關於我們</a>

那麼我們訪問到的頁面與使用下面訪問的頁面一樣

<a href="/about/about.php?id=22">關於我們</a>


2.php代碼

比如:http://www.xxxx.com/soft.php/1,100,8630.html

 代碼如下 複製代碼

< ?php
//利用server變數 取得PATH_INFO資訊 該例中為 /1,100,8630.html 也就是執行指令碼名後面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
//正則匹配一下參數
if(preg_match("//(d+),(d+),(d+).html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相當於soft.php?gid=1&sid=100&softid=8630
}else die('Path:Nothing!');
//就是這麼簡單了。~)
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.