使用.htaccess用RewriteRule替换项目

时间:2018-03-19 16:36:18

标签: .htaccess mod-rewrite

我正在尝试使用“RewriteRule”来重写Url。

我会使用此链接在我的博客中显示一篇新文章:

https://www.example.com/article/how-to-visit-italy-in-winter

原始脚本(包括变量)是

https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter

这是我的.htaccess

RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]

但它不起作用,浏览器重定向到:

  

https://www.example.com/post.php?idpost=

我试图解决大约2天没有成功:( 谢谢。

1 个答案:

答案 0 :(得分:0)

除了两件事,你的规则看起来很好。由于您没有捕获任何URI,因此$1后引用未定义且为空。要捕获uri的一部分,您需要使用正则表达式捕获组(.*)。 2)如果要静默重定向URL,则需要使用绝对路径而不是完整URL作为RewriteRule的目标。

RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]