我想设置 .htaccess 来重写:

example.com/bla/info/

example.com/info/index.php?q=bla

example.com/bla/info/txt/

example.com/info/index.php?q=bla&t=txt

我希望浏览器仍然显示:

example.com/bla/info/txt/

我不想在2级重写中重写,例如:

example.com/bla/xxx/ 

example.com/ccc/zzz/aaa/

example.com/silly/info/ 

可以和

一样好用
example.com/strange/info/mytxt/

这有意义吗?

任何帮助?

有帮助吗?

解决方案

如果你用^开始你的模式并用$结束它,那么规则只适用于整个字符串匹配的情况。

RewriteRule   ^/bla/info/$      /info/index.php?q=bla
RewriteRule   ^/bla/info/txt/$  /info/index.php?q=bla&t=txt

如果您使用不使用[R]选项,浏览器中显示的URL将是用户输入的内容。

你是否想要达到这个通用目的?如果是这样,您可以使用正则表达式匹配和变量替换。要使'bla'匹配任何字符串直到下一个斜杠(/),请使用

([^/]+)

在您的模式中,并在替换时以$ 1引用它。

RewriteRule ^/([^/]+)/info/$          /info/index.php?q=$1
RewriteRule ^/([^/]+)/info/([^/]+)/$  /info/index.php?q=$1&t=$2

我推荐有关mod_rewrite的Apache网页以获取更多信息

[编辑。修正了规则,在原始海报中找不到模式中的主机名。]

- 点击 BMB

其他提示

你让我走上正轨。

我最终得到了类似的东西:

RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]

非常感谢

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top