htaccess 重写规则用于抓取主机名之后的所有内容

发布于 2024-09-09 00:04:27 字数 296 浏览 1 评论 0原文

我想要一个 htaccess 重写规则来获取主机名之后的所有内容并将其用作参数。例如,我希望 http://example.com/onehttp://example.com/category.php?cat=one

我认为这应该很简单,但我不太能找到组合。 谢谢

I want an htaccess rewrite rule to grab everything past the host name and use it as an argument. For example, I want http://example.com/one to be handled by http://example.com/category.php?cat=one

I think it should be simple, but I can't quite find the combination.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-09-16 00:04:27

这就是我想到的:

RewriteRule ^/?([^\./]*)[:;,\.]*$ category.php?cat=$1 [L,NS]

有人认为有什么方法可以改进它吗?

This is what I came up with:

RewriteRule ^/?([^\./]*)[:;,\.]*$ category.php?cat=$1 [L,NS]

Anybody see any way to improve it?

亚希 2024-09-16 00:04:27

尝试这样的操作:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ category.php?cat=$0 [B]

如果您也想传递查询字符串,请务必将 B 标志更改为 B,QSA,但请注意有人可能会传递GET 参数 cat,它将覆盖您在重写中设置的内容。如果您需要的话,有一些解决方法可以解决这种情况,但除此之外,这应该适合您。

Try something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ category.php?cat=$0 [B]

If you want to pass the query string along too, be sure to change the B flag to B,QSA, but be aware that someone could then pass the GET argument cat, which would override what you set in the rewrite. There are some workarounds to that situation if you need them, but otherwise that should do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文