【问题标题】:Replace items with RewriteRule using .htaccess使用 .htaccess 将项目替换为 RewriteRule
【发布时间】:2018-03-19 16:36:18
【问题描述】:

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

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

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

原来的脚本(包括vars)是

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 天但没有成功的问题:( 谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    你的规则看起来不错,除了两件事。您的 $1 后向引用未定义且为空,因为您没有捕获任何 URI。要捕获部分 uri,您需要使用 Regex capture-groups (.*) 。 2) 如果您想静默重定向 url,则需要使用绝对路径而不是完整 url 作为 RewriteRule 的目的地。

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

    【讨论】:

    • 谢谢这是完美的:) 我需要研究正则表达式条件.. 我看到目标页面将显示为“example.com/post.php?idpost=how-to-visit-italy-in-winter”,是否也可以更改浏览器的 url? (导航栏)。我可以将其他代码添加到 .taccess 中吗?非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多