My question is a simple one, but I can't seem to find the answer. I'm currently working on some URL rewriting for a website, but I have encountered a problem. Currently the most basic rule I have goes something like this:
我的问题很简单,但似乎无法找到答案。我目前正在为网站进行一些URL重写,但我遇到了一个问题。目前我最基本的规则是这样的:
RewriteRule ^([a-zA-Z]+)/(([a-zA-Z]+)/?$ index.php?mod=$1&com$2
This works in most cases, and I have some special cases for where this doesn't apply, however one of the pages needs to pass a lot of information through the URL, and I want to automatically rewrite this. Some examples:
这在大多数情况下都适用,我有一些特殊情况不适用,但其中一个页面需要通过URL传递大量信息,我想自动重写。一些例子:
website.com/asdf/jkl/id/5/page/2
should become website.com/index.php?mod=asdf&com=jkl&id=5&page=2
website.com/asdf/jkl/id/5/page/2应该成为website.com/index.php?mod=asdf&com=jkl&id=5&page=2
website.com/qwer/yuio/search/keyword/sort/alpha
should become website.com/index.php?mod=qwer&com=yuio&search=keyword&sort=alpha
website.com/qwer/yuio/search/keyword/sort/alpha应成为website.com/index.php?mod=qwer&com=yuio&search=keyword&sort=alpha
Is this possible? I could really use some help here... Thanks! :)
这可能吗?我真的可以在这里使用一些帮助......谢谢! :)
2 个解决方案
#1
Depending on what language/framework you're using, it may be simpler to put the rewriting/dispatch in the script rather than attempt to do everything in mod_rewrite.
根据您正在使用的语言/框架,将重写/分派放在脚本中而不是尝试在mod_rewrite中执行所有操作可能更简单。
For example, if you were using PHP, given the URL:
例如,如果您使用PHP,则给定URL:
http://www.example.com/asdf.php/jkl/id/5/page/2
a script at asf.php could read the PATH_INFO variable, split it on slashes, and write the values into the expected place in $_REQUEST.
asf.php中的脚本可以读取PATH_INFO变量,将其拆分为斜杠,并将值写入$ _REQUEST中的预期位置。
Then all you need is one simple rewrite rule to elide the ‘.php’.
然后你只需要一个简单的重写规则来消除'.php'。
#2
You could use a recursive rule:
您可以使用递归规则:
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?mod=$1&com$2 [L,QSA]
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)/?(.*) $1/$4?$2=$3 [QSA]
But it sure would be easier to parse the request path with PHP.
但是使用PHP解析请求路径肯定会更容易。
#1
Depending on what language/framework you're using, it may be simpler to put the rewriting/dispatch in the script rather than attempt to do everything in mod_rewrite.
根据您正在使用的语言/框架,将重写/分派放在脚本中而不是尝试在mod_rewrite中执行所有操作可能更简单。
For example, if you were using PHP, given the URL:
例如,如果您使用PHP,则给定URL:
http://www.example.com/asdf.php/jkl/id/5/page/2
a script at asf.php could read the PATH_INFO variable, split it on slashes, and write the values into the expected place in $_REQUEST.
asf.php中的脚本可以读取PATH_INFO变量,将其拆分为斜杠,并将值写入$ _REQUEST中的预期位置。
Then all you need is one simple rewrite rule to elide the ‘.php’.
然后你只需要一个简单的重写规则来消除'.php'。
#2
You could use a recursive rule:
您可以使用递归规则:
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?mod=$1&com$2 [L,QSA]
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)/?(.*) $1/$4?$2=$3 [QSA]
But it sure would be easier to parse the request path with PHP.
但是使用PHP解析请求路径肯定会更容易。