[英]url rewrite and redirect - php
我最近開發了一個使用php的網站,它通過get接受id作為輸入。 http://website.com/profile.php?id=123 。 它的舊時尚方式,我看過網站使用漂亮的網址,如http://website.com/username.html
所以為了歸檔這個,我設法做了一些研究並最終得到了這個代碼。 我也在查詢字符串中傳遞用戶名。 http://website.com/profile.php?id=123&username=test_user參數ID僅用作profile.php的輸入
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
我尋求的最終輸出是http://website.com/username.html
他們有什么不妥嗎? 這似乎不起作用。 難道我做錯了什么 ? 如果是這樣,請善意讓我知道我的錯誤在哪里。 謝謝。
您可以使用此規則:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]
試試這條規則:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
那么你應該可以使用
http://website.com/profile/123/username.html
作為URL。 您可以忽略profile
部分,但我不推薦它,因為它會與您網站的其他網址沖突。
如果您只需要用戶名,請使用
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
並且URL將是
http://website.com/profile/username.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.