1

我正在开发一个私人项目,该项目基本上是一个配置文件系统。现在我一直在尝试如何改进 URL 格式。

目前,我正在使用如下所示的 URL:

-> http://example.com/overview
-> http://example.com/profile/?name=foo
-> http://example.com/jobs/?name=foo

overview 和 profile 都是我网站上的目录,其中包含一个 index.php 文件,其中包含应该从数据库中检索的 PageID。

现在,我的目标是将 URL 格式化为:

-> http://example.com/foohttp://example.com/profile/foo
-> http://example.com/foo/jobshttp://example.com/profile/foo/jobs

有没有办法用 MOD_REWRITE 做到这一点?

这意味着原始 url 看起来像http://example.com/?character=foo/jobs,即http://example.com/get_var/directory

我已经对 Stackoverflow 和 Google 进行了研究,搜索“ mod_rewrite get variables ”。但似乎没有什么是我希望看到的。

此致,
Larssy1

4

1 回答 1

1

要在斜杠之间提取 URI 的一部分并将其附加为 URL 参数,请使用表达式([^/]+)捕获直到但不包括下一个的所有/字符$1

RewriteEngine On
RewriteRule ^profile/([^/]+)/([^/]+/?)?$ profile/$2?character=$1 [L]

上面的规则将动态捕获Foo您示例中的内容,这意味着无论它后面是/jobsor/other还是/somethingelse,它都会被附加为/profile/somethingelse?character=Foo. 如果这不是必需的,并且/jobs是静态的,则无需将其捕获到$2

RewriteEngine On
# Don't dynamically capture what comes after the first variable group...
RewriteRule ^profile/([^/]+)/jobs$ profile/jobs?character=$1 [L]
于 2013-05-05T23:54:50.847 回答