我是 regex 和 Apache mod_rewrite 的新手,我嘗試了幾種方法,它不能轉換為 URL 友好,我不知道我做錯了什么。
我想在這些情況下從不友好的查詢字串轉換為友好的 URL:
場景一:從:https ://example.com/page.php?page_url=life-is-good 到:https ://example.com/life-is-good/
我的.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/$ /page.php?page_url=$1 [L,R=301]
場景 2:從:https ://example.com/resources/?blog.php?blog_url=life-is-good 到:https ://example.com/blogs/life-is-good/
我的.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(blogs)/(.*)/$ blogs/blog.php?blog_url=$2 [L,R=301]
非常感謝您對此的任何幫助。
uj5u.com熱心網友回復:
像這樣:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/ (blogs)/blog\.php\?blog_url=([^\s&] ) [NC]
RewriteRule ^ /%1/%2/? [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/ page\.php\?page_url=([^\s&] ) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# ignore all the requests for existing files and directories
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# internal forwards from pretty URL to actual one
RewriteRule ^(blogs)/(.*)/$ $1/blog.php?blog_url=$2 [L,QSA,NC]
RewriteRule ^(.*)/$ page.php?page_url=$1 [L,QSA]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/434001.html
