Apache mod_rewrite 没有做任何事情(?)
我在使用 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您拥有的所有内容中取出
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 ServerFaultUseCanonicalName off
。如果名称不是它所期望的,则重写不会发生。).
匹配任何字符。在它们之前添加一个反斜杠。 (小错误。不应导致重写失败,但它们也会匹配“mysite-com”等内容。)DocumentRoot
指令在 Apache 中设置,并且不需要成为 URL 的一部分,除非您在同一域上有多个应用程序(在这种情况下,它是应用程序根目录) ; 文档根目录不变)。更新:
^/?login
而不是^/login
)。^/?(\w+)
将匹配 /doc_root/login.php,并导致重写 /doc_root/doc_root.php。您的模式末尾可能应该有一个 $。UseCanonicalName off
. If the name isn't what it's expecting, the rewrite won't happen.).
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.)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:
^/?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.