Apache mod_rewrite 从不友好的查询字符串到友好的 URL

Apache mod_rewrite from unfriendly query string to a friendly URL

我是正则表达式和 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= [L,R=301]

场景二: 来自: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= [L,R=301]

非常感谢对此的任何帮助。

像这样:

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)/(.*)/$ /blog.php?blog_url= [L,QSA,NC]

RewriteRule ^(.*)/$ page.php?page_url= [L,QSA]