2015-04-18 45 views
-1

我有這樣的規則.htaccess用於重寫規則的.htaccess

RewriteRule ^([^/]*)$ /user.php?username=$1 [L] 

輸出:

http://domain.tld/username 

所需的輸出:

http://domain.tld/user/username 

我怎樣才能做到這一點?

+0

輸入是http:// domain.tld/user.php?username = bob,輸出是http:// domain.tld/user/bob?或以其他方式? – Drakes

回答

0

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代碼:

Options -MultiViews 
RewriteEngine On 
RewriteBase/

RewriteRule ^user/([^/]+)/?$ user.php?username=$1 [L,NC,QSA] 
+0

內部服務器錯誤 服務器遇到內部錯誤或配置錯誤,無法完成您的請求。 – User

+0

註釋掉「Options」行並重新測試。 – anubhava

+0

你能再幫我一次嗎?它不會加載圖像和樣式,需要我添加../到處? – User

2

這裏是如何改變一個URL的查詢字符串的路徑:

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/user.php [NC] 
RewriteCond %{QUERY_STRING} ^.*username=([a-zA-Z]+).*$ [NC] 
RewriteRule ^(.*)$ /user/%1? [L] 

輸入

http://domain.tld/user.php?username=bob 

輸出

http://domain.tld/user/bob 

這裏是如何走另一條路:

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/user/ [NC] 
RewriteRule ^user/(.+)$ /user.php?username=$1 [L] 

輸入

http://domain.tld/user/bob 

輸出

http://domain.tld/user.php?username=bob 

你可以測試.htaccess規則在這裏:http://htaccess.madewithlove.be/