简体   繁体   English

Angular和Laravel Apache / htaccess规则

[英]Angular and Laravel Apache / htaccess Rules

I am deploying an Angular and Laravel Application on Apache2 / Ubuntu 14. 我正在Apache2 / Ubuntu 14上部署Angular和Laravel应用程序。

I am having issues with the rewrites to make this work ok. 我在重写方面遇到问题,无法正常工作。

The angular application is always redirected to the index it also needs to call some laravel API routes. 角度应用程序总是被重定向到索引,它也需要调用一些laravel API路由。

I am only using laravel for a few API routes 我仅将laravel用于一些API路由

I am using Angular with UI Router in HTML5 mode. 我在HTML5模式下将Angular与UI Router一起使用。

I have set the laravel application to render: 我将laravel应用程序设置为呈现:

 Route::get('/', function () {
     return File::get(public_path().'\index.html');
 });

In Apache or HTaccess 在Apache或HTaccess中

Should i render the Angular HTML file or render the index.php which renders the Angular HTML file? 我应该渲染Angular HTML文件还是渲染index.php来渲染Angular HTML文件?

My Angular HTaccess file is Options -MultiViews 我的Angular HTaccess文件是Options -MultiViews

 RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

My Apache Virtual Host File is: 我的Apache虚拟主机文件是:

  AllowOverride All
  RewriteEngine On
  RewriteBase /var/www/Website/public/
   #       RewriteCond     %{REQUEST_URI} ^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
 #       RewriteCond     %{REQUEST_FILENAME} !-f
 #       RewriteCond     %{REQUEST_FILENAME} !-d
 #       RewriteRule     ./index.html [L]
 #      RewriteCond %{REQUEST_FILENAME} !-d
 #      RewriteCond %{REQUEST_FILENAME} !-f
 #      RewriteRule ^ index.php [L]

#     # 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]

I was trying a few options with this. 我正在尝试一些选择。

I have various API routes which the angular app calls on laravel, they all start with /api and i would like to exclude these from the angular app redirection using UI State Ref 我在laravel上有角度应用程序调用的各种API路由,它们都以/ api开头,我想使用UI状态引用从角度应用程序重定向中排除这些路由

Any ideas guys? 有想法吗?

Also Should i be using server vhosts instead of a htaccess file as it is slower? 还应该使用服务器虚拟主机而不是htaccess文件吗,因为它比较慢?

Thanks for your help guys :) First time poster :) 谢谢您的帮助:)第一次张贴海报:)

It's a little bit late, but I was stuck with the same issue and probably if someone else find this answer, could be useful. 有点晚了,但是我遇到了同样的问题,如果别人找到了这个答案,可能会有用。 I just realized why this happens and assuming this combinations works, you must be bootstraping your angular app from laravel service which is only used as API service. 我只是意识到了为什么会发生这种情况,并且假设这种组合有效,那么您必须从仅用作API服务的laravel服务中引导角度应用程序。 The problem come arise when angular shares the same base url with laravel. 当angular与laravel共享相同的基本URL时,就会出现问题。

For example, your laravel is hosted and start in X server from an url like this http://yourdomain.com/public/ which is the same base url for angular app since you copied all content index.html (angular app) and pasted to the main blade template (laravel app) that usually is called welcome.blade.php. 例如,您的laravel是托管的,并在X服务器中从类似http://yourdomain.com/public/的URL开始,因为您复制了所有内容index.html(Angular应用程序)并粘贴,所以它是angular应用程序的基本URL到通常称为welcome.blade.php的主刀片模板(laravel应用)。 This issue must be happening since you are not using the hash (#) in your angular routes. 由于您没有在角度路线中使用井号(#),因此必须发生此问题。

Probably in somewhere in your code you have something like : 可能在代码中的某处您有类似以下内容:

RouterModule.forRoot(ROUTES) 

instead of 代替

RouterModule.forRoot(AppRoutes, { useHash: true })

Another way to solve this is by using HashLocationStrategy that can be implemented as follows: 解决此问题的另一种方法是使用HashLocationStrategy,该实现可以如下实现:

In app.module.ts: 在app.module.ts中:

Add imports: 添加进口:

import { HashLocationStrategy, LocationStrategy } from '@angular/common';

And in NgMoudle provider, add: 在NgMoudle提供程序中,添加:

{provide: LocationStrategy, useClass: HashLocationStrategy}

Example (app.module.ts): 示例(app.module.ts):

import { NgModule }       from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule],
    providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
    bootstrap: [AppComponent],
})
export class AppModule {}

Both methods your urls gonna be like: 您的网址的两种方法都将类似于:

http://yourdomain.com/public/#/route and that additional hash (#) symbol is what avoid the URL conflict. http://yourdomain.com/public/#/route和该附加的井号(#)符号可以避免URL冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM