【问题标题】:How to properly write my .htaccess to ignore a folder如何正确编写我的 .htaccess 以忽略文件夹
【发布时间】: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


【解决方案1】:

尝试:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/backend/
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

【讨论】:

  • 使用这部分代码,主页得到404,后端重定向到返回404的主页。如果我在URL栏中手动访问,所有其他页面仍然有效。注意:
  • 您是否清除了缓存或尝试使用其他浏览器?
  • 是的,它没有工作......有趣的事实是,当我使用初始 htaccess 手动输入完整链接时,一切都很好。我认为这可能是导致重定向的“Angular.js html5mode”问题。
  • 我的 Angular 应用程序在 ROOT 上运行,我的代码基于此示例 Angular.js html5mode 来设置我的 .htaccess。但是如果用户访问 /backend/ 我使用其他类型的东西,我想忽略当前规则。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2011-06-20
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多