Angular和PHP .htaccess文件重定向问题 [英] Angular and PHP .htaccess file redirecting problem

查看:48
本文介绍了Angular和PHP .htaccess文件重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在linux apache上部署托管有角度的前端和php slim应用程序框架后端.

I am deploying on linux apache hosting angular frontend and php slim app framework backend.

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

In localhost I have on localhost:4200 angular and localhost:80 in xampp apache php

路径可行

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

now I have www.mydomain.org/ .(all deployed angular ) .(.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();
      

      我的问题是,我不明白如何将所有路径链接在一起.

      My question is, I can not understand how link all path together.

      因为Angular,需要.htaccess导致无法正常工作刷新页面我以各种不同的方式尝试过但只能正常工作,所以我需要在index.html上使用

      Because Angular, needs that .htaccess cause without not works refresh page I tried in all different ways but is only works, so I need that on index.html

      但是现在可以刷新,但是我不能使用php后端.

      But now works refresh but I can't use php backend.

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

      How can I use it, I need edit .htaccess, where index.php have to be on path?

      例如,考虑到api rest:由于Apache设置和文件主机localhost/slimapp/public/index.html/event(是从数据库检索事件的get调用),slimapp/event在我的电脑中

      Considering that for example api rest : slimapp/event is in my pc thanks to apache settings and file host localhost/slimapp/public/index.html/event (is a get call that retrieve event from db)

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

      How need to be new path? in real domain?

      推荐答案

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

      I use the exact same stack for writing applications ( Angular Front End / Slim API ). My first application ran into the same exact issue you were having, that being that both frameworks needed the .htaccess redirect and were fighting for control.

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

      My solution was rather simple. Angular being the front end controller i placed directly in my public facing folder ( in my case /public_html ). Slim was placed in a folder inside my public folder named /api.

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

      This way Angular has it's own .htaccess file in the public folder, and my API was accessible via the /public_html/api path and was able to have it's own .htaccess file.

      我的文件结构:

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

      应用程序网址: https://myapplication.com API URL: https://myapplication.com/api

      App URL: https://myapplication.com API URL: https://myapplication.com/api

      您的Angular .htaccess文件应包含重定向:

      Your Angular .htaccess should contain the redirect:

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

      您的Slim .htaccess文件应包含以下内容:

      Your Slim .htaccess should contain something like:

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

      这篇关于Angular和PHP .htaccess文件重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆