2012-07-24 85 views
2

我用查詢字符串中的幾個參數成功地改變了我的網址,使用mod重寫來清理看起來很醜的網址。但是,我的網站有很多網址。我試圖在.htaccess文件中編寫一個重定向函數,它會自動將舊URL重定向到新的重定向函數,而不是返回並編輯每個錨標記的href屬性。Mod用查詢字符串重寫URL到漂亮的網址

在我的.htaccess文件,我有以下幾點:

RewriteEngine On 

Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*) 

RewriteCond %{REQUEST_URI} !^(css|js|img)/ 
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L] 

沒有運氣,但...有什麼想法?

感謝

+0

我說得對,你想讓任何點擊「teams.php?league =。* ...」的人顯示爲「team/$ 1/...'? – 2012-07-24 12:53:29

+0

是的,你是。 100%正確 – Lance 2012-07-24 15:16:34

回答

3

你需要做的檢查,在它舊的URL與php實際上是被要求通過對%{THE_REQUEST}匹配,否則會永遠重定向環路(例如用戶進入team.php ,服務器重定向到團隊,瀏覽器請求團隊,服務器重寫爲teams.php,服務器看到「teams.php」並且重定向到團隊,瀏覽器請求團隊,服務器重寫爲teams.php等等)

RewriteEngine On 

# redirect when the user actually requests for teams.php 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+) 
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L] 

# internally rewrite any /teams/ URI 
RewriteCond %{REQUEST_URI} !^(css|js|img)/ 
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L] 
+0

爲什麼我們需要!^(css | js | img)/如果我用你的語法來重定向像teams.php這樣的文件,我應該複製/粘貼RewriteCond!^(css | js | img)每次,或在htaccess中足夠一次。謝謝:) – webtech 2015-08-02 20:40:38

+0

問問題的人不想重寫css,js或img文件夾的請求 – 2015-08-02 20:49:39

相關問題