【问题标题】:Url rewriting using RewriteCond whrere different url required based on query param使用 RewriteCond 重写 URL,根据查询参数需要不同的 url
【发布时间】:2021-08-09 01:50:20
【问题描述】:

我的要求是

  1. domain.com/src/home.php?page=category&category=recipe
  2. domain.com/src/home.php?page=article&key=myarticle
  3. domain.com/src/home.php

预期的结束网址

  1. domain.com/src/category/recipe

  2. domain.com/src/article/myarticle

  3. 域名.com

我写了一个

RewriteRule    ^src/([A-Za-z0-9-]+)/?$    src/home.php?page=article&key=$1    [NC,L] 

问题是,如果一个工作正常,那么另一个不工作。 任何人都可以根据“页面”查询参数建议如何使其在某些条件下工作。

提前致谢。

【问题讨论】:

    标签: regex .htaccess mod-rewrite


    【解决方案1】:

    使用您显示的示例,尝试,请尝试遵循 htaccess 规则文件。确保将您的 htaccess 文件与 src 文件夹(不在其中)一起保存。

    另外,请确保在测试 URL 之前清除浏览器缓存。

    RewriteEngine ON
    RewriteBase /src/
    ##Rules for handling url domain.com/src/category/recipe here.  
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
    
    ##Rules for handling urls like: domain.com/src/article/myarticle
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
    
    ##Rule for handling domain.com url here.
    RewriteRule ^/?$ src/home.php [QSA,L]
    

    【讨论】:

    • @Sroy,请告诉我我的解决方案是否对您有用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2017-11-07
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多