一旦我的朋友幫助我解決了這個問題,我就撰寫了這樣的代碼。example.com/blog-detail?slug=slugvalue它去哪里blog-detail.php,這個 urlexample.com/project?slug=test去project.php。
RewriteEngine ON
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S )\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
我認為它僅適用于博客詳細資訊頁面,現在我遇到了這個 url https://www.example.com/project?slug=test的問題,它將我重定向到 404 頁面,你能幫我嗎?
uj5u.com熱心網友回復:
使用您展示的樣本,嘗試;請嘗試遵循 htaccess 規則檔案。請確保在測驗您的 URL 之前清除您的瀏覽器快取。
我在這里添加了 2 個解決方案,所以請使用第一個規則集或使用第二個規則集,一次只設定一個。
第一種解決方案:為每個 php 檔案設定 2 個不同的規則,請嘗試以下規則。
RewriteEngine ON
###Rules for blog-detail.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(blog-detail)\?slug=(\S )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
###Rules for project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(project)\?slug=(\S )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ project.php?slug=$1 [QSA,L]
第二種解決方案:具有捕獲組的通用解決方案以一起處理兩種 php 檔案。
RewriteEngine ON
###Rules for blog-detail.php OR project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ /$1.php?slug=$2 [QSA,L]
注意1:請保留您的 php 檔案(project.php和blog-detail.php)應與您的 htaccess 規則檔案一起保存。
注意 2:對于 JS/CS 重寫/重定向:您可能需要使用 base 標簽來修復您的 js 和其他相關資源。如果您使用相對路徑鏈接 js 檔案,那么該檔案顯然會得到 404,因為它正在尋找 URL 路徑。例如,如果 URL 路徑是 /file/ 而不是 file.html,那么您的相關資源將從 /file/ 加載,這不是目錄而是重寫的 html 檔案。要解決此問題,請使您的鏈接成為絕對鏈接或使用基本標簽。在您的網頁標題中添加此<base href="/">內容,以便您的相關鏈接可以從正確的位置加載。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425147.html
