【问题标题】:Apache Rewrite rule to replace underscore with hyphen and append extension at endApache重写规则用连字符替换下划线并在末尾附加扩展名
【发布时间】:2012-11-02 06:28:43
【问题描述】:

我需要帮助来创建 apache 重写规则。我的要求如下。

来源网址:“http://localhost/directory/file_name_1”

目标 URL:“http://localhost/file-name-1.html”

注意:上述要求不适用于静态 URL,但可以用其他词代替“目录”和“文件名_1”。

  • “目录”一词应在新的重定向 URL 中删除
  • 下划线 (_) 应替换为连字符 (-)
  • “file-name-1”应以 .html 扩展名结尾。

感谢您的回复。

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    试试这个

    RewriteEngine On
    RewriteRule ^(/?.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
    RewriteRule ^(/?.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
    

    【讨论】:

    • 谢谢谢尔吉。如何在 URL 末尾附加扩展名。
    • 谢谢谢尔盖。它正在工作。但它只是从“_”重定向到“-”。我们如何重写它。我们还需要在我的 .htaccess 文件中添加什么其他内容吗?
    【解决方案2】:

    我已经使用了以下规则,现在它正在工作。

        RewriteEngine    On
        RewriteCond     %{REQUEST_URI}  !^/([A-Za-z0-9]+)/([A-Za-z0-9_]+)$
        RewriteRule     .*    - [S=3]
        RewriteRule     ^(/?.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
        RewriteRule     ^(/?.*/[^/]*?)_([^/_]*)$ $1-$2.html  [NC]
        RewriteRule     ^/(.*)/(.*)$        $2               [R,L]
    

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 2017-04-06
      • 2017-07-12
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      相关资源
      最近更新 更多