代码之家  ›  专栏  ›  技术社区  ›  dvijai

如何为具有不同值的相同参数重写htaccess url

  •  0
  • dvijai  · 技术社区  · 1 年前

    我有不同的动态页面URL和参数,如下所示。 PHP.htaccess页面。

    http://localhost/index.php?city=Delhi
    http://localhost/index.php?city=Delhi&page=AboutUs
    http://localhost/index.php?city=Delhi&page=Blog&Read=AnyBlog
    http://localhost/index.php?city=Delhi&page=Treatment&type=AnyTreatment
    

    我希望我的网址写得像下面这样。

    http://localhost/Delhi
    http://localhost/Delhi/AboutUs
    http://localhost/Delhi/Blog/AnyBlog
    http://localhost/Delhi/Treatment/AnyTreatment
    

    有人能帮忙重写代码吗?

    1 回复  |  直到 1 年前
        1
  •  0
  •   arkascha    1 年前

    最简单的是实现4个单独的重写规则:

    RewriteEngine on
    RewriteRule ^/?([^/]+)/Treatment/([^/]+)/?$ /index.php?city=$1&page=Treatment&Read=$2 [L]
    RewriteRule ^/?([^/]+)/Blog/([^/]+)/?$ /index.php?city=$1&page=Blog&Read=$2 [L]
    RewriteRule ^/?([^/]+)/([^/]+)/?$ /index.php?city=$1&page=$2 [L]
    RewriteCond %{REQUEST_URI} !^/index\.php$
    RewriteRule ^/?([^/]+)/?$ /index.php?city=$1 [L]
    

    这些规则同样适用,在http服务器的 中心的 虚拟主机配置文件,或者,如果您没有访问权限(即:如果您使用的是没有访问配置权限的廉价主机提供商),在 分布式 配置文件(通常称为“.htaccess”), 如果对http服务器的虚拟主机和位置启用了此类文件的考虑 两者都可行,第二种选择有很多缺点,但可能是任何人的唯一选择 自己操作http服务器。

    你可以简单地测试自己: htaccess tester by "made with love"

    您可能希望在前两条规则中添加“不区分大小写匹配”标志,以不区分大小记的方式匹配术语“博客”和“治疗”。只需将旗帜添加到现有的 L 标志: [NC,L]