- RewriteCond: 使用 RewriteCond 可以添加條件,只有滿足條件的請求才會被 RewriteRule 重寫。例如:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
- 捕獲組:可以使用捕獲組在重寫規則中引用匹配到的內容。例如:
RewriteRule ^blog/(.*)$ article.php?slug=$1 [L]
- 后向引用:可以在重寫規則中使用后向引用來引用捕獲組中的內容。例如:
RewriteRule ^category/([0-9]+)/(.*)$ archive.php?cat_id=$1&name=$2 [L]
- RewriteMap:通過使用 RewriteMap 可以實現更復雜的重寫規則,例如從外部文件讀取重寫規則。例如:
RewriteMap lc int:tolower
RewriteRule ^([a-z]+)$ ${lc:$1}
- 環境變量:可以通過設置環境變量來在重寫規則中進行判斷和操作。例如:
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule ^(.*)$ /maintenance.html [L]
- 文件系統檢測:可以使用 RewriteRule 來檢測文件系統中的文件或目錄是否存在,并根據結果進行重寫。例如:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]