【问题标题】:Angular and PHP .htaccess file redirecting problemAngular 和 PHP .htaccess 文件重定向问题
【发布时间】:2019-11-19 15:17:05
【问题描述】:

我在 linux apache 托管 angular 前端和 php slim 应用程序框架后端进行部署。

在 localhost 中,我在 xampp apache php 中有 localhost:4200 angular 和 localhost:80

路径有效

现在我有 www.mydomain.org/ .(所有部署的角度) .(.htaccess) .folder (slimapp)*php

在我的文件夹内:

  • 公开
    • index.php
    • 配置
      • db.php
    • 路线
      • event.php,login.php,user.php

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]

index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
header('Access-Control-Allow-Headers: token, Content-Type');

require_once '../vendor/autoload.php';
require_once '../slimapp/src/config/db.php';

// 客户路线

$app = new \Slim\App;
require_once "../slimapp/src/routes/user.php";
require_once "../slimapp/src/routes/login.php";
require_once "../slimapp/src/routes/event.php";


$app->run();

我的问题是,我无法理解如何将所有路径链接在一起。

因为 Angular,需要 .htaccess 导致刷新页面我尝试了各种不同的方式但只能工作,所以我需要 index.html

但现在可以刷新但我不能使用 php 后端。

如何使用它,我需要编辑 .htaccess,其中 index.php 必须在路径上?

考虑到例如 api rest : slimapp/event 在我的电脑中,这要归功于 apache 设置和文件主机 localhost/slimapp/public/index.html/event(是从 db 检索事件的 get 调用)

如何需要新路径?在真实域中?

【问题讨论】:

    标签: php angular apache .htaccess


    【解决方案1】:

    我使用完全相同的堆栈来编写应用程序(Angular Front End / Slim API)。我的第一个应用程序遇到了与您完全相同的问题,即两个框架都需要 .htaccess 重定向并且正在争夺控制权。

    我的解决方案相当简单。 Angular 是我直接放在面向公众的文件夹中的前端控制器(在我的情况下为 /public_html)。 Slim 被放置在我的公共文件夹内的一个文件夹中,名为/api

    这样,Angular 在公共文件夹中有自己的 .htaccess 文件,我的 API 可以通过 /public_html/api 路径访问,并且能够拥有自己的 .htaccess 文件。

    我的文件结构:

    /public_html
       .htaccess ( ANGULAR )
       { Angular Files }
       /api
         .htaccess
         { Slim Files ( index.php ) }
    

    应用网址:https://myapplication.com 接口地址:https://myapplication.com/api

    你的 Angular .htaccess 应该包含重定向:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ https://yourapp.com/index.html [QSA,L]
    

    你的 Slim .htaccess 应该包含如下内容:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
    

    【讨论】:

    • 非常感谢这对我有帮助:)
    • 没问题!您能否将我的帖子标记为答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多