【发布时间】:2017-02-24 02:23:57
【问题描述】:
我一直在寻找有关我正在尝试做的事情的回答者。我有一个将 html5mode 设置为 true 的 Angular.js 应用程序。所以我创建了一个 .htaccess 文件以便能够运行。
到目前为止,我有这个。
# Rewrite Engine
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
我在文件夹 /backend/ 中有一个管理面板,当我尝试访问它时,我不断重定向到 /index.html。
我认为这可能与:
'-d' (is directory) 将 TestString 视为路径名并进行测试 是否存在,是否为目录。
到目前为止,我尝试向文件添加新条件,但到目前为止我只收到 500 个内部错误。
# Rewrite Engine
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !^/backend/
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
我的目标:如果我站在 /backend/ 文件夹页面中,让 .htaccess 忽略 RewriteRule。
感谢您的帮助。
【问题讨论】:
-
RewriteCond %{REQUEST_URI} !^/folder/to/exclude. -
你能提供更多关于在哪里放置它的信息吗?我的 .htaccess 的实际结构?当我放置您的代码时,当我访问 /backend/ 时它确实有效,但我的首页上现在没有显示。它说找不到文件。编辑:这很奇怪,因为没有这条规则,如果我手动编写 URL,它可以工作,但是如果我点击管理链接,我会被重定向。
-
你不应该在规则中使用
DOCUMENT_ROOT。您正在处理 URL,并且文档根目录中的路径部分永远不会暴露给外界。 rewrite 仅适用于 url 中的内容,例如example.com/foo/bar/baz.html将有foo/bar/baz.html用于重写,文件物理存在于/home/sites/example.com/html/foo/bar/baz.html中的事实无关紧要。 -
当我删除
DOCUMENT_ROOT时,即使没有添加新的 RewriteCond,我也会立即获得内部 500。 -
只有一个
%{REQUEST_URI}(不是2个):RewriteCond %{REQUEST_URI} !^/backend/
标签: php angularjs apache .htaccess mod-rewrite