【问题标题】:(Recursive) Regular expression for .htaccess file(递归).htaccess 文件的正则表达式
【发布时间】:2011-01-19 21:56:48
【问题描述】:

我正在尝试修改我的网页,我目前使用 QueryStrings 来显示页面,并希望将网站 URL 更改为“搜索引擎友好”的。

我是正则表达式和 .htaccess 文件的新手,我想更改 URL

http://someaddress.bla/index.php?lang=en&rootCategory=$rootCaty&leafCategory=$leafCat

http://someaddress.bla/en/$rootCat/$sub-category/$sub-category2/.../$leafCat

类别以递归方式存储在数据库中,因此我必须在 URL 中显示每个类别级别。但我需要的只是网址中的最后一个类别。例如,该类别可能是一个根类别,例如:

http://someaddresss.bla/en/Company

当前从 URL 显示的内容:

http://someaddress.bla/?lang=en&RootCategory=Company

或者它可能是一个子类别,例如:

http://someaddress.bla/en/Company/AboutUs

当前从 URL 显示的内容:

http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=AboutUs

或者可能是一片叶子:

http://someaddress.bla/en/Company/AboutUs/Staff/.../Steve 其中“Steve”是递归的叶子。 此页面当前从 URL 显示:

http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=Steve

我无法编写正则表达式来处理这种情况,我们将不胜感激。谢谢!

【问题讨论】:

  • 您是否考虑过 mod_rewrite 将所有 404 请求写入单个 PHP 文件并在该文件中执行您的正则表达式?
  • 我不明白你在说什么“mod_rewrite all 404 requests to a single PHP file”,但我有几个php文件,我可以检查内容是否丢失用 php。我打算将正则表达式更改为 "RewriteRule ^/?([^/]+)/([^/]+)/?$ someFile.php?lang=$1&RootCategory=$2 RewriteRule ^/?([^ /]+)/([^/]+)(/.+)?/([^/]+)/?$ someFile.php?lang=$1&RootCategory=$2&LeafCategory=$4" 我在 someFile 中处理数据的地方。 php

标签: regex .htaccess mod-rewrite seo url-rewriting


【解决方案1】:
RewriteRule ^/?([^/]+)/([^/]+)/?$ /?lang=$1&RootCategory=$2
RewriteRule ^/?([^/]+)/([^/]+)(/.+)?/([^/]+)/?$ /?lang=$1&RootCategory=$2&LeafCategory=$4

【讨论】:

    【解决方案2】:

    您可以通过锚定到 $(“字符串结尾”字符)来匹配 URL 中的最后一项:

    ([^/]+)$
    

    将匹配并捕获 URL 中不是 / 字符的最后一组字符。

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多