2010-12-09 26 views
0

什麼是以下URL重寫了的.htaccess代碼:阿帕奇重寫:DIR/file.php富=酒吧和名稱= VAL - > file.php newparam = DIR&富=酒吧和名稱= VAL

www.example.com/dir/file.php 

成?

www.example.com/file.php?newparam=dir 

www.example.com/dir/file.php?param1=val1&param2=val2 

www.example.com/file.php?newparam=dir&param=val1&param2=val2 

在此先感謝。

+0

@Keng mod_rewrite URL重寫使用regexp來匹配現有的URL。 – Orbling 2010-12-09 13:50:56

+0

請使用問題的描述性標題。有[噸](http://stackoverflow.com/questions/941010/apache-url-rewriting)[問題](http://stackoverflow.com/questions/285383/apache-mod-rewrite-htaccess)有關到[URL](http://stackoverflow.com/questions/2920816/apache-mod-rewrite-question)[重寫](http://stackoverflow.com/questions/3430488/apache-url-rewrite-problem)在apache,其中大多數與非描述性標題。 – Lekensteyn 2010-12-09 14:04:26

回答

0

試試下面,就讓我知道,如果它的工作原理:

RewriteEngine on 
RewriteBase/

# Allow files that exist to bypass rewrites 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Rewrite remaining files 
RewriteRule ^/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+\.php)$ /$2?newparam=$1 [L,QSA] 

這僅允許dirfile.php包含字母數字字符,下劃線和破折號。

最終規則查找匹配正斜槓之間的兩個路徑組件的URI。第一個必須是給定集合[-a-zA-Z0-9_]+中的一個或多個字符,第二個必須是相同的,但以.php結尾。 ^$字符匹配URI的前面和後面。替換表示將第二個方括號組(file.php部分)放在前面,並將第一個方括號組(dir部分)作爲名爲newparam的參數。末尾的[L,QSA]表示這是最後的[L]規則(如果匹配),所以停止進一步匹配並執行重定向,並且[QSA]的意思是「查詢字符串追加」,它將原始查詢字符串再次添加到末尾,而將該字符串關閉刪除原始查詢字符串。