2012-12-18 111 views
1

URL轉發這是我目前的.htaccess有兩個參數

RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?u=$1 [L] 
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

原來www.example.comexample.com並解釋example.com/username AS example.com /index.php?u=username

現在我想通過第二個參數一樣example.com/index.php?u=username & E =電子郵件,仍然保持格式example.com/arg1 & ARG2。我如何在.htaccess中做到這一點?

回答

0

首先,你需要之前移動重定向你的路由規則:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

然後,你要檢查的2論證路線:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([0-9a-zA-Z-]+)/([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA] 

(這個假設的URL看起來像:http://example.com/username/emailaddress,但如果你真的想&將它們分開,使用這個規則,而:

RewriteRule ^([0-9a-zA-Z-]+)&([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA] 

而這個假設看起來像網址:http://example.com/username&emailaddress

現在你原來的規則:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([0-9a-zA-Z-]+)/?$ /index.php?u=$1 [L,QSA] 
+0

這個工作當我通過這兩個參數。當我只傳遞一個參數時,如何使它具有通用性? –

+0

@LeonardoDaVintik如果它的工作原理,根據你的問題,你應該考慮[** **接受(http://stackoverflow.com/faq#howtoask)答案第一。 –

+0

@LeonardoDaVintik你說的意思是「普遍的情況下,當我通過只有一個ARG」?你原來的規則需要處理1個參數的情況。上面那個,2個參數請求,頂部是重定向。 –