【问题标题】:.htaccess rewrite rule for complex url structures.htaccess 复杂 url 结构的重写规则
【发布时间】:2016-10-07 11:42:47
【问题描述】:

我有以下网址:

www.example.com/category/news/article-name

我需要删除category/news 并重定向到:

www.example.com/article-name

我有以下 RewriteRule 工作:

RewriteRule ^category/news/(.*)$ https://example.com/$1 [R=301,L]

但是,我需要编辑上面的 RewriteRule 以允许在不重定向的情况下加载下面的 URL:

www.example.com/category/news/

当前此 URL 重定向到 www.example.com

这可能吗?

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    只需在您的规则中将.*(0 个或多个字符)更改为.+(1 个或多个字符):

    RewriteRule ^category/news/(.+)$ /$1 [R=301,L,NC,NE]
    

    现在它不会影响/category/news/ URL。

    请记住在测试此更改时清除浏览器缓存。

    【讨论】:

      【解决方案2】:

      尝试重写而不是重定向,在这种情况下,您可以调用 www.example.com/article-name 内部重写为 www.example.com/category/news/article-name

      请检查。

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond ^([\w-]+)$ category/news/$1 [L]
      

      【讨论】:

      • 感谢您的回答,但这不起作用。我收到内部服务器错误。但是,这确实有效: RewriteRule ^category/news/(.+)$ /$1 [R=301,L,NC,NE]
      猜你喜欢
      • 2019-08-09
      • 2011-12-06
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多