seo友好url的htaccess重写规则问题

Problem with htaccess rewrite rule for seo friendly url

提问人:arman 提问时间:9/2/2023 更新时间:9/2/2023 访问量:22

问:

经过一个月的尝试,我无法解决问题:

我正在尝试重定向此 URL:

https://www.example.com/news.php?post=hello-world

到这个 URL:

https://www.example.com/news/hello-world

我的代码不起作用并说:

404 未找到。

在此服务器上找不到请求的资源!

RewriteCond %{THE_REQUEST} /news\.php\?post=([^\s&]+) [NC]
RewriteRule ^ /news/%1? [R=302,L,NE]
正则表达式 apache .htaccess mod-rewrite

评论


答:

2赞 anubhava 9/2/2023 #1

您缺少一个额外的内部重写规则,无法静默地将漂亮的 URL 转发到内部工作 URL。

像这样:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /news\.php\?post=([^\s&]+) [NC]
RewriteRule ^ /news/%1? [R=302,L,NE]

# internally rewrite /news/123 to /news.php?post=123
RewriteRule ^news/([^/]+)/?$ news.php?post=$1 [NC,L,QSA]