【问题标题】:Replace underscore with slash in the URL and cut the extension将 URL 中的下划线替换为斜线并剪切扩展名
【发布时间】:2015-11-19 05:57:32
【问题描述】:

我有一个文件,http://example.com/house_location.phtml。我需要调用 URL 字符串 http://example.com/house/location(没有结束斜线)并且该 URL 必须执行 house_location.phtml。

所以我需要将“_”替换为“/”并剪切扩展名“.pthml”。

我最后一次尝试是:

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+house_([^/]+)\.phtml
RewriteRule ^ /house/%1? [L,R]
RewriteRule ^house/([^/]+)$ /house_$1.phtml [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.phtml [NC,L]

但它不起作用。我该如何解决?

【问题讨论】:

    标签: php regex .htaccess


    【解决方案1】:

    这就是重写规则的样子

    RewriteRule ^/?([a-z]+)/([a-z]+)$ $1_$2.phtml
    

    (写在手机上)

    【讨论】:

    • 什么不使用.+而不是[a-z]+,以防查询中有数字?
    • 然后也添加数字。作为程序员,您知道您对数据的期望。如果你使用 .+ 甚至 //// 将被匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多