【发布时间】: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