Apache Rewrite规则用连字符替换下划线并在末尾附加扩展名 [英] Apache Rewrite rule to replace underscore with hyphen and append extension at end

查看:65
本文介绍了Apache Rewrite规则用连字符替换下划线并在末尾附加扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need help to create apache rewrite rule. My requirement is as given below.

源URL:"http://localhost/directory/file_name_1"

Source URL: "http://localhost/directory/file_name_1"

目标网址:"http://localhost/file-name-1.html"

Destination URL: "http://localhost/file-name-1.html"

注意:上面的要求不是静态URL,但是除了目录"和"file_name_1"以外,还会有其他任何单词.

Note: Above requirement is not for static URL, but there would be any other word instead of "directory" and "file_name_1".

    应该在新的重定向URL中删除
  • 目录"一词
  • 下划线(_)应替换为连字符(-)
  • 文件名-1"应以.html扩展名结尾.
  • "directory" word should be removed in new redirected URL
  • underscore ( _ ) should be replaced with Hyphen ( - )
  • "file-name-1" should be ended with .html extension.

感谢您的答复.

推荐答案

我使用了以下规则,现在可以使用了.

I have used below rules and now it is working.

    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]

这篇关于Apache Rewrite规则用连字符替换下划线并在末尾附加扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆