您好,過去 3 天我一直在使用 htaccess,似乎無法弄清楚為什么我的 htaccess 檔案沒有正確更改我的 url。
所以我試圖把這個查詢字串 url 從www.example.com/test/user-profile.php?id=4變成www.example.com/test/user-profile/4
url 中的 /test/ 不是一個檔案,它是 public_html 主目錄中的一個檔案夾。
這是我的 htaccess 檔案
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.] )$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /test/user-profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
截至目前,它正在訪問 url,www.example.com/test/user-profile/4但它沒有加載整個頁面。只是卡住了,謝謝你的時間
uj5u.com熱心網友回復:
使用您展示的樣品,嘗試;請嘗試遵循 htaccess 規則檔案。在測驗您的 URL 之前,請確保清除瀏覽器快取。
RewriteEngine ON
RewriteBase /
##RewriteBase /test/
##Extrenal redirect rule.
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\.php\?id=(\d )\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ $1/$2.php?id=$3 [QSA,L]
JS/CS 重寫/重定向:
您可能需要使用 base 標簽來修復您的 js 和其他相關資源。如果您使用相對路徑鏈接 js 檔案,那么該檔案顯然會得到 404,因為它正在尋找 URL 路徑。例如,如果 URL 路徑是 /file/ 而不是 file.html,那么您的相關資源將從 /file/ 加載,它不是目錄,而是重寫的 html 檔案。要解決此問題,請使您的鏈接成為絕對鏈接或使用基本標簽。在您網頁的標題中添加此內容,<base href="/">以便您的相關鏈接可以從正確的位置加載。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/387982.html
標籤:阿帕奇 .htaccess 改写 url重写 请求参数
