[英]htaccess with url rewriting not working
這是我的htaccess代碼:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)$ http://www.example.com/index.php?id=82&user=$1 [L,R=301]
顯然,我希望將此URL:www.example.com/username轉換為http://www.example.com/index.php?id=82&user=username
但是,這不起作用。.(此代碼導致htaccess根本無法正常工作,並出現“找不到頁面”錯誤。
如果我將] + $更改為] +? 該代碼可以工作,但不像我想要的那樣:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301]
導致將URL重寫/重定向到http://www.example.com/index.php?id=82&user=index ...完全一樣,因此使用user = index。
現在,如果我刪除RewriteRule。* /index.php行,則htaccess再次不再起作用,導致頁面未找到錯誤...
我花了很多天來解決這個問題,但我絕對一無所知。
因此,我只希望www.example.com/username重定向到http://www.example.com/index.php?id=82&user=username
這里有幾個問題。
/typo3
或^/([a-zA-Z0-9_-]+)?
前斜線一樣^/([a-zA-Z0-9_-]+)?
([a-zA-Z0-9_-]+)?
也匹配空的請求,因為結尾的?
。 我想這不是您想要的。 [R]
或添加[L]
標志,否則規則將按順序處理。 這就是為什么首先將請求重寫為index.php
,然后在下一個規則中將index
識別為用戶,然后再次將其重寫為.../index.php?id=82&user=index
.*
和([a-zA-Z0-9_-]+)
之間的下一個問題。 .*
識別所有請求,包括每個用戶。 因此,無法區分用戶和任何其他請求。 要重寫用戶名,您可以嘗試
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+) http://www.example.com/index.php?id=82&user=$1 [L]
這意味着,如果請求與現有文件!-f
或目錄!-d
不對應,則假定它是用戶名並重寫為index.php?...
如果您不想重定向,請忽略主機名
RewriteRule ^([a-zA-Z0-9_-]+) /index.php?id=82&user=$1 [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.