【问题标题】:How to rewrite url in apache htaccess for angularjs app如何在 apache htaccess 中为 angularjs 应用程序重写 url
【发布时间】:2016-05-10 21:41:19
【问题描述】:

我使用的htaccess如下

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} -U
RewriteRule ^.*$ - [NC]

RewriteCond %{REQUEST_URI} !-U
RewriteRule ^(.*)$ #/$1 [L]

在我的本地主机上,当我只是输入 localhost/ 时,它会重定向到 localhost/home 并且我的 Angular 应用程序运行良好。但是当我直接输入 url localhost/home 时,它​​会抛出 404。 我需要将其重写为 localhost/#/home

在我的 Angular 应用程序中,在 index.html 标头中,我添加了 $locationProvider.html5mode(true),html5 模式正在工作,但唯一的问题是刷新浏览器时...

提前谢谢大家。

【问题讨论】:

    标签: angularjs apache .htaccess mod-rewrite


    【解决方案1】:
    RewriteEngine   On
    RewriteBase     /
    RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
    RewriteCond     %{REQUEST_FILENAME} !-f
    RewriteCond     %{REQUEST_FILENAME} !-d
    RewriteRule     ./index.html [L]
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案2】:

    我在我的虚拟主机上处理这个问题

    <VirtualHost *:8888>
        DocumentRoot "/Applications/MAMP/htdocs/angular"
        ServerName  zeyton.dev
        ServerAlias www.zeyton.dev
        DirectoryIndex index.html
      <Directory "/Applications/MAMP/htdocs/angular">
        order allow,deny
        allow from all
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^(.*) /index.html [NC,L]
      </Directory>
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      相关资源
      最近更新 更多