用于角度js的apache 2的重写规则

问题描述 投票:17回答:5

显然,整个网络上都有很多mod重写讨论和答案。但是,我很难抓住它们。所以我想我会问这里。

我要求重写规则来做Andy Joslin在评论中所解释的:https://stackoverflow.com/a/11100438

这是我在example.com根目录下的当前目录结构

  • 应用程序/
  • app / index.html(角度js应用程序的“根”)
  • api(这将是一个用于'api'的symfony 2应用程序。我将从角度向这里发送ajax请求。)

我想将所有请求重定向到app / index.html,除了对/ api的请求。

例如:

http://example.com/categories/electronics/ipod实际上就像去http://example.com/app/index.html/categories/electronics/ipod

我想隐藏app / index.html部分。

然后,对http://example.com/api的请求会有例外,因为我需要向这些url路径发出ajax请求。

感谢您提供的所有帮助/指导。

apache mod-rewrite angularjs rewrite
5个回答
14
投票

这里有一些东西让你去(把它放在你的/.htaccess文件中):

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api

# otherwise forward it to index.html 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^app/. /app/index.html [NC,L]

24
投票

这个问题的公认答案已经过时了。您现在可以在Apache 2.2.16+的conf文件中使用FallbackResource指令。

FallbackResource /app/index.html

如果您希望FallbackResource指令忽略“/ api”路由:

<Directory /api>
FallbackResource disabled
</Directory> 

0
投票

这个答案的问题是你仍然需要404s文件未找到。根据一个人如何构建他们的前端应用程序,人们常常让css或其他文件返回404,特别是如果他们正在构建更大的动态应用程序,或者正在外包前端工作。此外,角度路由通常与文件系统上的任何内容无关。如果将其缩小到单个目录,则上述操作可能有效。例如,我经常对与文件系统布局(/ ajs /)无关的所有角度路由使用公共前缀。如果你能做到的话

<Directory /ajs>
FallbackResource /app/index.html
</Directory> 

然后它会更有意义,但这似乎对我没有用。使用公共前缀也可以使后端规则更加简单,无论后端如何。例如,如果您不使用反向代理,则可以设置简单的服务器转发控制器。它使得对apache重写规则的建模变得简单。例如:

RewriteEngine On
RewriteRule ^/ajs/(.+)$ /index.html

也就是说,之前我还没有看过目录/回退方法,所以有兴趣探索它,因为我需要的唯一重写是角度前进。谢谢!


0
投票

这是Scott Ferguson的优秀答案的略微变化和细化。我发现使用<Location>指令比使用<Directory>更容易。 <Directory>指令采用绝对路径名,在不同的机器中可以有不同的名称。

所以,让我们说你的Angular应用程序的index.html位于web服务器文档根目录下的my-app/index.html中。并且您希望使用http://localhost/my-app访问该应用程序。

首先确保你的基础href是“/ my-app /”。接下来,在你的httpd.conf文件中添加:

<Location "/my-app">
    FallbackResource /my-app/index.html
</Location>

如果找不到/my-app/下的任何资源,这将导致Apache加载index.html。

如果您在相同路径下进行API调用,例如/my-app/api,那么您可能不应该为这些调用应用FallbackResource规则。所以添加:

<Location "/my-app/api">
    FallbackResource disabled
</Location>

0
投票

从我的生产服务器:

<VirtualHost *:80>
        ServerName XXX.com

        DocumentRoot /home/www/XXX.com/www

        # Local Tomcat server
        <Location /api/>
          ProxyPass http://localhost:8080/api/
          ProxyPassReverse http://localhost:8080/api/
        </Location>

        RewriteEngine On

        # If an existing asset or directory or API is requested go to it as it is
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
        RewriteCond %{REQUEST_URI} /api
        RewriteRule ^ - [L]

        # If the requested resource doesn't exist (and is not API), use index.html
        RewriteRule ^ /index.html

        ErrorLog logs/XXX.com-error.log
        CustomLog logs/XXX.com-access.log common
</VirtualHost>

请注意,“!”在接受的答案中“/ api”之前是不正确的。

© www.soinside.com 2019 - 2024. All rights reserved.