[英]url rewrite htaccess and php
我想重寫URL,為此,我得到了以下代碼。
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?uname=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?uname=$1
現在,當我編寫類似www.mywebsite.com/username的 url時,它可以正常工作,並且沒有出現任何錯誤。
現在,即使我一口氣寫www.mywebsite.com/index.php?uname=username ,也如何獲得該URL。
當我在URL中寫入www.mywebsite.com/index.php?uname=username時,我想將www.mywebsite.com/index.php?uname=username更改為www.mywebsite.com/username 。
至於WWW的前綴重寫工作,我想同樣的方式來改變我的網址www.mywebsite.com/username即使用戶寫入www.mywebsite.com/index.php?uname=username。
以下應該工作:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?uname=$1
它將檢查文件或目錄是否不存在,如果不存在,則將假定它是用戶名並重寫URL。
如果要從index.php?uname=username
重定向到/username
則可以將其添加到index.php
的頂部:
if (strpos($_SERVER['REQUEST_URI'], "uname") !== false)
{
header("Location: /" . $_GET['uname']);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.