如何在apache htaccess中重写angularjs应用的url

时间:2021-03-18 11:13:46

The htaccess I'm using is as follow

我正在使用的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]

On my localhost, when I'm simply typing localhost/ it's redirecting to localhost/home and my angular app is running perfectly. But when I'm directly typing the url localhost/home it's throwing 404. I need to rewrite it as localhost/#/home

在我的localhost上,当我简单地输入localhost/它将重定向到localhost/home时,我的角化应用程序运行得很好。但是当我直接输入url localhost/home时,它会抛出404。我需要将它重写为localhost/#/home

In my angular app, in index.html header, I've added and $locationProvider.html5mode(true), html5 mode is working, but only problem is when refreshing browser...

在我的角化应用,索引。html头,我添加了$locationProvider.html5mode(正确)html5模式正在工作,但唯一的问题是当刷新浏览器…

Thank in advance everyone.

提前谢谢大家。

2 个解决方案

#1


2  

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


0  

I handle this issue in my project with working on my VirtualHost

在我的项目中,我使用虚拟主机处理这个问题

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

#1


2  

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


0  

I handle this issue in my project with working on my VirtualHost

在我的项目中,我使用虚拟主机处理这个问题

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