apache僞靜態規則解析

apache僞靜態規則解析php

最近有個客戶有個要求,昨天折騰了一會,沒解決,今天沒啥就多學習學習html

仍是根據例子來學習比較快nginx

1 簡單的重定向規則apache

RewriteEngine On  //啓動規則
RewriteBase /       //根目錄啓動僞靜態
RewriteRule ^index/$ index.php  //訪問index/ 那麼就是訪問index.php
RewriteRule ^register/$ /s_youka/register.html  //訪問register  就是訪問/s_youka/register.html

 

2  稍微複雜一點的api

RewriteEngine on
RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$1&id=$2
//後面的 $1 $2 對應前面的()內的代碼  其餘的是正則規則
//^表明開頭$表明結束 [0-9]+ 是多個0到9之間的數字 \是轉義後面的.

 

3  更精準一些的服務器

/type.php?typeid=* –> /type*.html
/type.php?typeid=*&page=* –> /type*page*.html
RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]
//重點介紹下PT 交給下一條處理

1) R[=code](force redirect) 強制外部重定向
強制在替代字符串加上http://thishost[:thisport]/前綴重定向到外部的URL.若是code不指定,將用缺省的302 HTTP狀態碼。
2) F(force URL to be forbidden)禁用URL,返回403HTTP狀態碼。
3) G(force URL to be gone) 強制URL爲GONE,返回410HTTP狀態碼。
4) P(force proxy) 強制使用代理轉發。
5) L(last rule) 代表當前規則是最後一條規則,中止分析之後規則的重寫。
6) N(next round) 從新從第一條規則開始運行重寫過程。
7) C(chained with next rule) 與下一條規則關聯
若是規則匹配則正常處理,該標誌無效,若是不匹配,那麼下面全部關聯的規則都跳過。
8) T=MIME-type(force MIME type) 強制MIME類型
9) NS (used only if no internal sub-request) 只用於不是內部子請求
10) NC(no case) 不區分大小寫
11) QSA(query string append) 追加請求字符串
12) NE(no URI escaping of output) 不在輸出轉義特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 將能正確的將/foo/zoo轉換成/bar?arg=P1=zoo
13) PT(pass through to next handler) 傳遞給下一個處理
例如:app

複製代碼 代碼以下:
RewriteRule ^/abc(.*) /def$1 [PT] # 將會交給/def規則處理
Alias /def /ghi


14) S=num(skip next rule(s)) 跳過num條規則
15) E=VAR:VAL(set environment variable) 設置環境變量學習

 

4  discuz3x的規則this

RewriteEngine On  //開啓
RewriteBase / //當前根目錄
RewriteCond %{QUERY_STRING} ^(.*)$  //定義了規則生效的條件-查詢字符串
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
//topic-開頭 .htm後綴  實際上市訪問了 後面的地址

 

5 nginx相似spa

      Nginx下設置僞靜態方法與Apache差很少,直接在nginx.conf (或者在對應的*.conf) 中找到需設置僞靜態規則的服務器對應字段,在server{ location/{ } }中添加如下代碼:

server {
    listen 80 default_server;
    server_name _;
    location / {
         root /usr/share/nginx/html;
         index index.html index.htm;
         rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3;
         }
}

 


添加後重啓Nginx服務便可生效!

 

PS:若是你的僞靜態不起做用,請檢測文件的名字是否是 .htaccess 切記

相關文章
相關標籤/搜索