2012-08-07 55 views
1

我的.htaccess中有以下代碼。Apache mod_rewrite模塊問題

RewriteEngine On 
RewriteRule ^/(\w+)/?$ /?user=$1 

我試圖改寫 http://domain.com/?user=usernamehttp://domain.com/username。不幸的是,這段代碼不會重寫任何內容。請幫忙

注意: 我檢查了phpinfo()並加載了mod_rewrite。

更新

我需要從URL獲取用戶名像http://facebook.com/username。但是這段代碼重寫了根文件夾中的每個文件夾,所以我的/ css文件夾變成了http://domain.com/css/?u=common。如何讓這個代碼僅適用於http://domain.com/index.php

+0

你的URL看起來像你試圖重寫的http:// domain.com/username'(實際URL)'http://domain.com/?user = username'? – ThinkingMonkey 2012-08-07 13:46:37

+0

@ThinkingMonkey你認爲,問題是輸入和輸出順序錯誤? – treng 2012-08-07 13:48:19

+0

是的你的RewriteRule似乎很好。您正嘗試從瀏覽器訪問URL「http:// domain.com/username」嗎? – ThinkingMonkey 2012-08-07 13:52:18

回答

1

你正在做的錯誤是在該行^/(\w+)/?$

重寫規則剝掉從圖案的開始/的開始使用/中要匹配.htaccess和目錄上下文。

嘗試這樣做:

RewriteEngine On 
RewriteRule ^(\w+)/?$ /?user=$1 

RewriteRule Directive docs

什麼是匹配的?

在VirtualHost上下文中,模式最初將與主機名和端口後以及查詢字符串(例如「/app1/index.html」)之前的URL部分進行匹配。

在Directory和htaccess上下文中,在刪除引導服務器到當前RewriteRule的前綴(例如「app1/index.html」或「index.html」,具體取決於指令在哪裏定義)。

如果您希望與主機名,端口或查詢字符串匹配,請分別使用RewriteCond和%{HTTP_HOST},%{SERVER_PORT}或%{QUERY_STRING}變量。


編輯:答案更新每個操作的要求: 補充一點:

RewriteEngine On 

#do nothig if URL is trying to access the folder CSS. 
RewriteRule *css/* - [L] 

#checks where the URL is a valid file/folder. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(\w+)/?$ /?user=$1 
1

我認爲你是一輪做正確的方式,但是反過來解釋了錯誤的方式!

問題是你不需要初始/因爲傳遞給測試的URL不包括它!?

我懷疑它應該重寫規則^(\ w +)/ $ /?U = $ 1

此外,要小心你不循環結束了!