Apache 2,Zend 重定向到 Angular 2 App

Apache 2, Zend redirecting to Angular 2 App

我在将旧的 zend 网站与新的 Angular 2/4 应用程序合并时遇到问题。我想在 zend 中插入我的 angular 应用程序作为另一个 url。

所以我有域 http://example.net,它将我重定向到我的 zend 网站, 现在我想要 http://example.net/Angular,它将我重定向到我的 Angular 应用程序。

我在 zend public 文件夹中创建了另一个文件夹,并从 angular2 复制了所有已编译的文件。当我转到 http://example.net/Angular 时,一切都很好,我可以使用 links 浏览网站,但是当我刷新或发送更多嵌套的 link 时,例如 http://example .net/Angular/profile/12 我被重定向到 zend 404 页面。

Angular 应用.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

Zend .htaccess

Options +FollowSymLinks +ExecCGI


php_value upload_max_filesize 24M
php_value post_max_size 24M
php_value memory_limit 512M

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
#https:// -> https://www.
php_value safe_mode "0"

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteBase /

  RewriteCond %{HTTP_USER_AGENT} msnbot [NC]
  RewriteRule . - [F]

  RewriteCond %{HTTP_HOST} ^example.net [NC]
  #^ -> https://www.
  RewriteRule ^(.*)$ https://www.example.net%{REQUEST_URI} [R=301,L]
    #http -> https
  # newsletter stats collector
  RewriteCond %{REQUEST_URI} ^/ns/[0-9]+/[^/]+$
  RewriteRule ^(.*)$ _newsletter_stats.php [L]

RewriteRule ^action/order.php order.php [L]

  # newsletter tracker
  RewriteCond %{REQUEST_URI} ^/nt/[0-9]+$
  RewriteRule ^(.*)$ _newsletter_tracker.php [L]

  ErrorDocument 404 /404.html


  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteCond %{REQUEST_URI} !\.pdf$
  RewriteRule .* - [L]

  RewriteCond %{REQUEST_FILENAME} !-f


  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>


<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript application/x-javascript
</IfModule>

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|js|php|pl|jpg|png|gif|css)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

<IfModule mod_setenvif.c>
# Netscape 4.x has some problems�
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won�t work. You can use the following
# workaround to get the desired effect:
#BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don�t compress already-compressed files
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary
</IfModule>

<FilesMatch ".(ico|jpg|jpeg|png|gif|js|swf|css)$">
  <IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault A604800
    Header append Cache-Control "public"
  </IfModule>
  Header unset ETag
  FileETag None
</FilesMatch>

此应用程序中的路由系统可能不为编写此站点代码的人所知,因此我想坚持使用 htaccess 和 Angular 解决方案。

我也不能 运行 节点服务器或任何其他我已经拥有的 Apache 2...托管。

我肯定会建议为 angular 应用程序使用 Alias(即使在部署方面,这也意味着您将项目和工件分开)。

您提供的第一个 .htaccess 可能甚至没有被请求,因为您查询 document_root /(作为您的 ZF 应用程序),它验证文件是否存在于磁盘上, 看到 /Angular/profile/12 没有并服务于 index.php.

因此,另一个解决方案是在 ZF 中添加另一个条件 .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    #...

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(?!Angular/)(.+)$ /index.html/ [NC,QSA,L]

    #...
</IfModule mod_rewrite.c>

类似的东西(未测试)。


编辑:那个怎么样

# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

RewriteCond %{REQUEST_URI}:: ^(/.+)/Angular/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/Angular/index.html [L]

RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

同样,这可能需要一些调整,而其他解决方案,例如别名或每个网站的域(app.domain.com 和 api.domain.com)会更好。