我正在尝试使用Angular 4和PHP路由,但是我无法配置它以同时使用这两种情况。我可以使它与一个或另一个一起使用,但不能同时使用。
这是我的文件夹结构:
root
│ index.html
│ vendor.bundle.js
| [..other angular files..]
│
└─api
│ │ index.php
│ │ init.php
│ │
│ └─vendor
└─assets
└─[..other project files..]
在文件api/index.php
上是我设置php路由的地方。
所以,当我使用此.htaccess
时,PHP路线 works 正如预期的,但是当我刷新页面时,角路由不起作用,因为它将其视为PHP路线。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . api/index.php [L]
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
我尝试将.htaccess
更改为重定向,仅当称为url包含路由/api/
时,但它没有起作用。这样:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} api
RewriteRule . api/index.php [L]
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
使用此代码,即使我刷新页面,Angular路线也可以工作,但是PHP路由永远不会重定向到index.php文件,因此它始终返回null。
我需要做什么来解决此问题?
,由于我对HTACCESS不了解太多,我不知道这个答案是否正确,但我设法使它起作用。
我所做的是将行RewriteBase /
移至顶部并删除RewriteRule ^index.html..
,最终的.htaccess
看起来像这样:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} api
RewriteRule . api/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
现在这是按预期工作的。如果有人有更好的答案,我会打开问题。