How to rewrite url in apache htaccess for angularjs app

安稳与你 提交于 2019-12-20 03:34:22

问题


The htaccess I'm using is as follow

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

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

Thank in advance everyone.


回答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:


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>


来源:https://stackoverflow.com/questions/35147465/how-to-rewrite-url-in-apache-htaccess-for-angularjs-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!