php - htaccess 在 php 和 angularjs 中配置路由

标签 php .htaccess angularjs routes

所以基本上我想要根目录中有 2 个文件夹,一个是服务器,另一个是公共(public)文件夹。

文件夹服务器通过始终转到 server/routes.php 进行保护

只要该文件或目录存在,文件夹 public 就可以自由地转到任何地方。

如果 URL 是 localhost/server(anything),它将转到 server/routes.php,URL 为 (anything)

如果 URL 是localhost/(除服务器之外的任何内容) 并且(除服务器之外的任何内容)不是公开的文件或目录,则它将转到 public/index.html> 与 URL (除服务器之外的任何内容) 否则,如果 (除服务器之外的任何内容) 是公开的文件或目录,则它将转到 public/(除服务器之外的任何内容),即(文件或目录)

一些例子:

  • 本地主机/服务器
    • 服务器/routes.php
    • $_SERVER['REQUEST_URI'] 为空
  • 本地主机/服务器/用户
    • 服务器/routes.php
    • $_SERVER['REQUEST_URI'] 是用户
  • localhost/server/users?orderby=名称
    • 服务器/routes.php
    • $_SERVER['REQUEST_URI'] 是 users?orderby=name
  • 本地主机
    • 网址为空
    • public/index.html
  • 本地主机/用户
    • url 是用户
    • public/index.html
  • localhost/partials/users.html (该文件存在于 public/partials/users.html 中)
    • public/partials/users.html

我如何在 .htaccess 中执行此逻辑?

最佳答案

将此.hatccess放在文件夹server上:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /server/

RewriteRule ^routes\.php$ - [L]
RewriteRule . routes.php [L]
RewriteRule ^$ routes.php [L]

并将此 .htaccess 放在您域的根文件夹中:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d [NC]
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/index.html [L]

RewriteCond %{DOCUMENT_ROOT}/public/$1 -d [NC,OR]
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteCond %{REQUEST_URI} !^/public/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]

尝试一下这 2 组 .htaccess 并记住,如果您早期使用 301 重定向尝试过其他任何操作,建议您清除浏览器缓存或使用您没有使用过的其他浏览器不用来浏览这些网站以避免缓存。

关于php - htaccess 在 php 和 angularjs 中配置路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18926947/

相关文章:

javascript - 如何在 Javascript Laravel Blade 中编写路由 URL?

wordpress - 来自子域的字体已被跨域资源共享策略阻止

javascript - 将 Angular 服务与 es6 模块一起使用是否有意义?

javascript - 否则如果在 Angular js模板中

php - 为什么我的login.php 不会重定向我?

php - 1064 你在 Doctrine 中的 SQL 语法有错误。为什么?

php - 如何使用htaccess在nginx的子文件夹中运行index.php?

javascript - Angular 项目架构

php - SQL 语法错误 - PHP/MySQL

.htaccess - htaccess RewriteRule 重定向