【问题标题】:Angular 2 apache .htaccess file 404 on subpages子页面上的 Angular 2 apache .htaccess 文件 404
【发布时间】:2025-11-23 00:00:02
【问题描述】:

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

【问题讨论】:

  • 可能有助于显示网络服务器日志或您请求的逐字 URL。
  • 请求 URL example.com/register 我收到此错误:在此服务器上找不到请求的 URL /register。
  • GET /register HTTP/1.1" 404 501 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0. 3396.99 Safari/537.36

标签: php angular apache .htaccess angular-routing


【解决方案1】:

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

– 在 Apache 服务器托管中,编辑 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 service apache2 restart 重启Apache服务器

【讨论】:

    【解决方案2】:

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

    你是说如果请求的文档不是文件 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
    

    【讨论】:

    • 还是不行,刷新页面时出现404错误The requested URL /search/professionals was not found on this server.
    • 是的,当我转到 example.com 并且当我通过 [routerLink]="['/register']" 单击导航中的链接时,它也有效,但是当我在示例中时。 com/register 并刷新页面我收到 404 错误,但当我通过 [routerLink]="['/register']" 转到 example.com/register 时它不起作用,但当我刷新页面时不起作用
    【解决方案3】:

    试试这个

    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/

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        最近更新 更多