htaccess 重定向除文件/目录之外的所有内容

发布于 2024-07-13 08:05:02 字数 671 浏览 6 评论 0原文

我正在尝试创建一个重写规则来完成两件事:

  1. (www.)?domain.com 重定向到 log.domain.com
  2. 让任何其他目录或文件请求无需重定向即可通过domain.com

这不是Apache 服务器(它是LiteSpeed),但据说它完全支持.htaccess 文件。

这是我对规则的看法:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule .* http://log.domain.com/

发生的情况是,如果我请求特定文件(例如 dir/index.php),它会被允许通过,但目录请求仍然会重定向到 log.domain.com。 我认为第二个 RewriteCond 的 $ 会阻止这种情况发生,但事实似乎并非如此。

编辑:仅出于存档目的,我的目的不是将任何不存在的目录/文件重定向到 log.domain.com,因为用户永远不必这样做。 这使得规则的标准变得更加简单,混沌和秋葵都通过下面的第一条规则得出了这一点。 再次感谢!

I am trying to create a rewrite rule that accomplishes two things:

  1. Redirect (www.)?domain.com to log.domain.com
  2. Let any other directory or file request on domain.com through without redirection

This not an Apache server (it's LiteSpeed), but supposedly it supports .htaccess files wholly.

This was my shot at the rule:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule .* http://log.domain.com/

What's happening is that if I request a specific file (like dir/index.php) it's let through, but directory requests are still redirected to log.domain.com. I thought the $ of the second RewriteCond would keep this from happening, but that seems to not be the case.

EDIT: Just for archival purposes my intention was not to redirect any nonexistent directories/files to the log.domain.com since users should never have to do that. This made the criteria a lot simpler for the rule, which chaos and gumbo both arrived at with their first rules below. Thanks again!

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

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

发布评论

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

评论(2

谎言月老 2024-07-20 08:05:04
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^$ http://log.domain.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^$ http://log.domain.com/
聊慰 2024-07-20 08:05:04

尝试这个:

# skip the next rule if host is not example.com or www.example.com
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S]

# redirect if requested URI does not match a regular file or directory
# or if the requested URI path is just "/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ http://log.example.com/

Try this:

# skip the next rule if host is not example.com or www.example.com
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S]

# redirect if requested URI does not match a regular file or directory
# or if the requested URI path is just "/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ http://log.example.com/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文