子页面上的Angular 2 apache .htaccess文件404

use*_*331 5 php apache .htaccess angular-routing angular

我已经在我的apache服务器上成功安装并运行了Angular 2应用程序,我可以浏览页面 [routerLink]="['/register']"

但是,当我刷新页面时,我在注册页面上收到404错误.我的重写规则有问题吗:

Options +FollowSymLinks
<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L]
</ifModule>
Run Code Online (Sandbox Code Playgroud)

这也是我的apache VirtualHost

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/front/dist/browser
    <Directory /var/www/html/front>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

xyz*_*xyz 0

是的,据我了解,您的重写规则对我来说似乎不正确..

您是说,如果请求的文档不是文件,也不是目录,而且我猜不是索引页,那么重定向到index.html,如果是怎么办?为什么您不想转到索引页呢?我相信RewriteCond %{REQUEST_URI} !index这意味着你不想去索引。

我不知道你的要求是什么,但为了简单的要求,我使用:

    RewriteCond %{REQUEST_FILENAME} -s [OR]   # don't redirect for files which phycically exit
    RewriteCond %{REQUEST_FILENAME} -d  # don't redirect for directories
    RewriteRule ^.*$ - [NC,L]  # if the above condition(s) satisfy then do nothing
    RewriteRule ^.*$ index.html [NC,L] # for everything else, redirect to index.html
Run Code Online (Sandbox Code Playgroud)