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



我已经在我的 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>

这里也是我的apache虚拟主机

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

您可以通过将 404 错误重定向到索引来解决它.html

– 在 Apache

服务器托管中,编辑 Apache 配置:

sudo nano/etc/apache2/sites-enabled/000-default.conf

– 添加错误文档 404,如下所示: 我的索引.html 位于/var/www/html/vas/index.html

– 完整文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
# redirect 404 to index page
ErrorDocument 404 /vas/index.html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

– 通过 cmd sudo service apache2 重新启动 Apache 服务器

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

你是说如果请求的文档不是文件也不是目录我想不是索引页面,然后重定向到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

试试这个

RewriteEngine on
# 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]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html  

检查这个:https://github.com/angular/angular-cli/issues/1630

参考:https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

尝试使用以下规则在应用程序的根目录上创建一个 .htaccess 文件,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
RewriteRule ^ /index.html

最新更新