Apache mod_rewrite 没有做任何事情(?)

发布于 2024-09-30 03:55:49 字数 765 浏览 0 评论 0原文

我在使用 Apache 的 mod_rewrite 时遇到了一些问题。我试图让它做的一件事是隐藏一些实现细节,例如,用户可以看到 URL http://www.mysite.com/login 但 Apache 响应页面 http://www.mysite.com/doc_root/login.php(最好不要向用户显示它是 PHP 文件或目录结构)。这是我的 .htaccess 文件中的内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com*
RewriteRule ^/(\w+) /doc_root/$1.php [L]

#Redirect http://www.mysite.com to the login page
RewriteRule ^/?$ https://www.mysite.com/doc_root/login.php

但是当我转到 http://www.mysite.com/login< /a>,即使页面存在,我也会收到 404 错误。我显然对 mod_rewrite 条件和规则的工作原理不太了解,所以有人可以告诉我我做错了什么吗?谢谢。

I'm having some trouble with Apache's mod_rewrite. One of the things I'm trying to get it to do is hide some of my implementation details, so that, for example, the user sees the URL http://www.mysite.com/login but Apache responds with the page at http://www.mysite.com/doc_root/login.php instead (preferably without showing the user that it's a PHP file or the directory structure). Here's what I have in my .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com*
RewriteRule ^/(\w+) /doc_root/$1.php [L]

#Redirect http://www.mysite.com to the login page
RewriteRule ^/?$ https://www.mysite.com/doc_root/login.php

But when I go to http://www.mysite.com/login, I get a 404 error even though the page exists. I clearly don't have a great understanding of how the mod_rewrite conditionals and rules work, so can anyone please tell me what I'm doing wrong? Thanks.

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

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

发布评论

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

评论(2

旧时光的容颜 2024-10-07 03:55:49

从您拥有的所有内容中取出doc_root。这将为您提供所需的结果。但是我不确定是否需要。如果某人手动输入 http://www.mysite.com/index.php,您将如何强制他们登录。 php

此外,如果您试图强制所有流量都使用 SSL,最好使用第二个 VirtualHost 和 Redirect 而不是 mod_rewrite。这些都是可能更适合 ServerFault 的问题

Take doc_root out of all the stuff you have it in. That will give you the result you're asking for. However I'm not sure if it's desired or not. How are you going to force someone to login if they manually type http://www.mysite.com/index.php?

Also if you're trying to force all traffic to SSL it's better to use a second VirtualHost and Redirect instead of mod_rewrite. Those are all questions probably better suited for ServerFault

带上头具痛哭 2024-10-07 03:55:49
  • 除非您的网站有一堆不同的域名,并且您希望 mysite.com 进行重写,否则您不需要 RewriteCond。 (潜在的问题。Apache 喜欢摆弄域名,除非您设置 UseCanonicalName off。如果名称不是它所期望的,则重写不会发生。)
  • 在 RewriteCond(和 RewriteRule)中模式,. 匹配任何字符。在它们之前添加一个反斜杠。 (小错误。不应导致重写失败,但它们也会匹配“mysite-com”等内容。)
  • mod_rewrite 实际上是一个 URL 到文件名过滤器。虽然它经常用于将 URL 重写为其他 URL,但有时如果您重写的是一个 URL 并且它无法辨别,它有时会出现错误。 (特别是如果它重写的内容是别名,或者不会直接转换为真实的文件名。)但是,如果您在规则中添加 [PT] 标志,它会将重写的内容视为 URL 并将其传递到其他过滤器(包括将 URL 转换为文件名的过滤器)。
  • 你真的需要“/doc_root”吗?文档根目录应该已经使用 DocumentRoot 指令在 Apache 中设置,并且不需要成为 URL 的一部分,除非您在同一域上有多个应用程序(在这种情况下,它是应用程序根目录) ; 文档根目录不变)。

更新:

  • 我刚刚想到的另一件事:重写规则在 .htaccess 文件中的工作方式不同。 Apache 喜欢去掉前导斜杠。因此,您可能希望删除模式中的第一个斜杠,或者至少将其设为可选(^/?login 而不是 ^/login)。
  • ^/?(\w+) 将匹配 /doc_root/login.php,并导致重写 /doc_root/doc_root.php。您的模式末尾可能应该有一个 $。
  • Unless your site has a bunch of different domain names, and you only want mysite.com to do the rewriting, you don't need the RewriteCond. (Potential problem. Apache likes to dick around with the domain name unless you set UseCanonicalName off. If the name isn't what it's expecting, the rewrite won't happen.)
  • In RewriteCond (and RewriteRule) patterns, . matches any character. Add a backslash before them. (Minor bug. Shouldn't cause rewrites to fail, but they would match stuff like "mysite-com" as well.)
  • mod_rewrite is actually a URL-to-filename filter. Though it is often used to rewrite URLs to other URLs, sometimes it will misbehave if what you're rewriting to is a URL and it can't tell. (Especially if what it's rewriting to would be an alias, or would otherwise not translate directly to a real filename.) If you add a [PT] flag onto your rule, though, it will consider the rewritten thing a URL and pass it along to the other filters (including the ones that turn URLs into filenames).
  • Do you really need "/doc_root"? The document root should already be set up in Apache using the DocumentRoot directive, and shouldn't need to be part of the URL unless you have multiple apps on the same domain (in which case it's the app root; the document root doesn't change).

UPDATE:

  • Another thing i just thought about: Rewrite rules work differently in .htaccess files. Apache likes to strip off the leading slash. So you will probably want to get rid of the first slash in your patterns, or at least make it optional (^/?login instead of ^/login).
  • ^/?(\w+) will match /doc_root/login.php, and cause a rewrite to /doc_root/doc_root.php. You should probably have a $ at the end of your pattern.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文