简体   繁体   English

使用htaccess重写URL会收到404错误-AngularJS html5模式

[英]URL rewrite with htaccess gets 404 error - AngularJS html5 mode

Angular js page refresh 404 error after removing # and ! 删除#和!后,Angular js页面刷新404错误。 from URL 来自URL

I working on an angular(1.7.5) project, in which I want to remove # and ! 我正在一个angular(1.7.5)项目中工作,我想在其中删除#和! from the URL, for that, I use the below code 从URL,为此,我使用以下代码

myApp.config(['$locationProvider', function ($locationProvider) {
     $locationProvider.html5Mode(true);
}]);

and added base tag in index.html in the root folder 并在根文件夹的index.html中添加了基本标记

<base href="/myApp/">

and .htaccess file 和.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

finally, I removed the # and ! 最后,我删除了#和! from the URL, but the issue is when I refresh the page or open the same URL in new tab the page get 404 error, 从URL,但问题是当我刷新页面或在新标签页中打开相同的URL时页面出现404错误,

http://localhost/dev/ ----- works fine

but

http://localhost/dev/login ----404 error 

I dont know how to resolve the issue help me thank you. 我不知道该如何解决该问题,谢谢您。

Try to modify the .htaccess rules like this : 尝试像这样修改.htaccess规则:

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

You need to modify your .htaccess . 您需要修改.htaccess The problem is that your current rules only forward the first path in the URL. 问题是您当前的规则仅转发URL中的第一个路径。 Instead, you need to have it forward all nested paths. 相反,您需要使其转发所有嵌套路径。

RewriteEngine On  
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteRule ^ /index.html [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM