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

时间:2018-08-06 19:27:07

标签: php angular apache .htaccess angular-routing

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

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

3 个答案:

答案 0 :(得分:1)

您可以通过将404错误重定向到index.html来解决此问题

–在Apache Server托管中,编辑Apache配置:

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

–像这样添加ErrorDocument 404: 我的index.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服务apache2 restart重新启动Apache服务器

答案 1 :(得分:0)

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

您是说所请求的文档不是文件 AND 还是目录 AND ,我猜不是索引页,然后重定向到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

答案 2 :(得分:0)

尝试一下

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/