apache重寫規則詳解

摘要:# Various rewrite rules.<IfModule mod_rewrite.c> RewriteEngine on # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redire…php

# Various rewrite rules.

<IfModule mod_rewrite.c>

  RewriteEngine on



  # If your site can be accessed both with and without the 'www.' prefix, you

  # can use one of the following settings to redirect users to your preferred

  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:

  #

  # To redirect all users to access the site WITH the 'www.' prefix,

  # (http://example.com/... will be redirected to http://www.example.com/...)

  # adapt and uncomment the following:

  # RewriteCond %{HTTP_HOST} ^example.com$ [NC]

  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

  #

  # To redirect all users to access the site WITHOUT the 'www.' prefix,

  # (http://www.example.com/... will be redirected to http://example.com/...)

  # uncomment and adapt the following:

  # RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]



  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a

  # VirtualDocumentRoot and the rewrite rules are not working properly.

  # For example if your site is at http://example.com/drupal uncomment and

  # modify the following line:

  # RewriteBase /drupal

  #

  # If your site is running in a VirtualDocumentRoot at http://example.com/,

  # uncomment the following line:

  # RewriteBase /



  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.

  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_FILENAME} !-d

  RewriteCond %{REQUEST_URI} !=/favicon.ico

  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</IfModule>

 

RewriteEngine on
爲重寫引擎開關,若是設爲off,則任何重寫規則定義將不被應用,該開關的另外一好處就是若是爲了臨時拿掉重寫規則,則改成off再重啓動Apache便可,沒必要將下面一條條的重寫規則註釋掉。html

 

 

RewriteCond 條件重寫規則,當知足後面定義的條件後纔會應用下面的重寫規則。java

RewriteCond 的語法以下: web

RewriteCond  TestString CondPattern [flags] 正則表達式

TestString 服務器

TestString 是一個純文本的字符串,可是也能夠包含一些擴展的成分,這裏就是經過 %{NAME_OF_VARIABLE} 引用的服務器變量。 app

服務器變量: 函數

REQUEST_FILENAME  是與請求相匹配的完整的本地文件系統的文件路徑名。 測試

REQUEST_URI 是在HTTP請求行中所請求的資源(好比:"/index.html")。 ui

CondPattern

CondPattern是條件模式,即一個應用於當前TestString實例的正則表達式。TestString將被首先計算,而後再與CondPattern匹配。

好比: RewriteCond %{REQUEST_URI} !=/favicon.ico  的意思就是請求的地址不是 /favicon.ico 才觸發下面的地址重寫;

! (驚歎號)來指定不匹配。
'-d'(目錄)  將TestString視爲一個路徑名並測試它是否爲一個存在的目錄。
'-f'(常規文件)  將TestString視爲一個路徑名並測試它是否爲一個存在的常規文件。

因此: RewriteCond %{REQUEST_FILENAME} !-f  的意思爲, 請求的文件並不存在時觸發後面的地址重寫;

RewriteCond %{REQUEST_FILENAME} !-d  的意思爲: 請求的目錄並不存在時,觸發後面的地址重寫;

更多這個參數的寫法能夠參看: http://www.52web.com/52article/?view-258.html

[flags]

咱們還能夠在CondPattern以後追加特殊的標記[flags]做爲RewriteCond指令的第三個參數。flags是一個以逗號分隔的如下標記的列表:

'nocase|NC'(忽略大小寫)
它使測試忽略大小寫,擴展後的TestString和CondPattern中'A-Z' 和'a-z'是沒有區別的。此標記僅用於TestString和CondPattern的比較,而對文件系統和子請求的檢查不起做用。
'ornext|OR'(或下一條件)
它以OR方式組合若干規則的條件,而不是隱含的AND。典型的例子以下:

 

 

RewriteRule 具體的重寫策略:

RewriteRule的語法如後:  RewriteRule Pattern Substitution [flags]

Pattern

對Apache1.2及之後的版本,模板(Pattern)是一個POSIX正則式,用以匹配當前的URL。當前的URL不必定是最初提交的URL,由於可能用一些規則在此規則前已經對URL進行了處理。

這裏是 ^(.*)$ ,意味着一行任何的地址請求;

Substitution

當匹配成功後,Substitution會被用來替換相應的匹配。

這裏的 RewriteRule ^(.*)$ index.php?q=$1  意味着, 若是原先請求的是 /aaa/ddd  重寫後地址變成 index.php?q=/aaa/ddd ;

它除了能夠是普通的字符串之外,還能夠包括:
1. $N,引用RewriteRule模板中匹配的相關字串,N表示序號,N=0..9
2. %N,引用最後一個RewriteCond模板中匹配的數據,N表示序號
3. %{VARNAME},服務器變量
4. ${mapname:key|default},映射函數調用。

更多信息能夠參看:http://www.ixdba.net/article/ae/1443.html

[flags]

做爲RewriteRule指令的第三個參數。 Flags是一個包含以逗號分隔的標記的列表。

這裏的 L,QSA 分別是以下意思:

'last|L' (最後一個規則 last)
當即中止重寫操做,並再也不應用其餘重寫規則。 它對應於Perl中的last命令或C語言中的break命令。 這個標記能夠阻止當前已被重寫的URL爲其後繼的規則所重寫。 舉例,使用它能夠重寫根路徑的URL('/')爲實際存在的URL, 好比, '/e/www/'.

'qsappend|QSA' (追加請求串 query string append)
此標記強制重寫引擎在已有的替換串中追加一個請求串,而不是簡單的替換。 若是須要經過重寫規則在請求串中增長信息,就可使用這個標記。

好比說我要把 /user/ghj1976 重定向到/user.php?uid=ghj1976 那麼個人規則就須這樣:
RewriteRule ^user/([^/]+)$ ^/user.php?uid=$2 [L]

可是若是咱們但願 /user/ghj1976?key=java  重定向到  /user.php?uid=ghj1976&key=java

寫成 RewriteRule ^user/([^/]+)$ ^/user.php?uid=$2 [QSA,L]  這樣增長個 QSA 就能夠了。

若是沒有QSA,就會丟掉咱們額外傳的這個參數key=java。

相關文章
相關標籤/搜索