【问题标题】:How to remove a section of url with RewriteRule如何使用 RewriteRule 删除部分 url
【发布时间】:2018-04-07 16:40:56
【问题描述】:

我找到了Fryanswer on StackOverflow,它看起来可以回答我的问题,但我无法解决问题。

我有这个网址:

https://www.mywebsite.com/section1/section2/exercise/someurl

我想改写成:

https://www.mywebsite.com/section1/section2/someurl

我尝试了以下方法:

RewriteRule ^(.\*)/exercise/(.\*)$ https://%{SERVER_NAME}/$1/$2 [NC,R=301,L]

还有:

RewriteRule ^(.\*)/exercise/(.\*)$ $1/$2 [NC,R=301,L]

但是 URL 没有被重写。我打开了RewriteEngine,我将RewriteRule 用于其他规则,它们工作正常。

这里是link to the complete .htaccess file

有人有什么建议吗?谢谢。

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    看起来正则表达式与 URL 不匹配。

    您正在尝试将实际的星号与此 \* 匹配。

    您想要匹配所有内容,以便 . 匹配第一个字符。添加+,现在它匹配所有内容:.+

    \/这样转义正斜杠也是一个好主意

    试试看:^(.+)\/exercise\/(.+)$

    【讨论】:

    • 您好 nickstarob,感谢您的建议。我按照您的明确提示进行了操作:^(.+)\/exercise\/(.+)$ $1/$2 但 URL 仍未重写。这是需要更改的link
    • 完全重写了吗?编辑您的问题并包含整个 .htaccess 文件。
    • 嗨@nickstaroba,感谢您的帮助。将修改后的正则表达式与 RedirectMatch 而不是 RewriteRule 结合使用,它可以正常工作。我们目前正在使用:RedirectMatch 301 ^(.+)\/exercise\/(.+) $1/$2 我很想知道为什么会这样,但是:RewriteRule ^(.+)\/exercise\/(.+) $ $1/$2 [NC,R=301,L] 没有。谢谢
    • 我需要查看完整的 .htaccess 文件。 RedirectMatch 使用mod_alias。可能是您的 Apache 安装配置不正确(意味着 mod_rewrite 已禁用)。另外,您的 .htaccess 文件是否包含 RewriteEngine On 行?
    • Here is the link to the complete .htaccess file. .htaccess 包括 RewriteEngine On mod_alias 行在服务器上配置
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2011-01-10
    • 1970-01-01
    相关资源
    最近更新 更多