【发布时间】:2021-02-17 23:59:33
【问题描述】:
我想通过 htaccess 向 Angular 应用程序添加国家代码“en-gb”。文件夹“en-gb”不存在。我只希望它显示在 url 中并服务 index.html
-
当我点击 http://angular.wlocal/ 时,它应该将 url 更改为 http://angular.wlocal/en-gb 并从服务器的根文件夹中提供 index.html(而不在 url 中显示实际的 index.html 文件)
-
如果请求的文件/文件夹不存在,例如。 http://angular.wlocal/this-does-not-exist url 应该更改为 http://angular.wlocal/en-gb/this-does-not-exist 并从服务器上的根文件夹中提供 index.html (/index.html)
-
如果文件/文件夹存在,例如。 http://angular.wlocal/vendor.js - 网址不应更改,文件应相对于服务器上的根文件夹( /vendor.js )提供服务
-
这也应该适用于目录。例如。 http://angular.wlocal/css/style.css 应该在服务器上提供 /css/style.css。
我的 .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#does the folder AND file exist? (ie. /index.html & css/style.css)
#yes - serve it & pop out of htaccess
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
#does the url already have a country flag set?
#no - add country flag (ie. http://angular.wlocal/ -> http://angular.wlocal/en-gb)
RewriteCond %{REQUEST_URI} !/en-gb
RewriteRule ^(.*)$ en-gb/$1
#if file folder not found, serve index.html (ie. http://angular.wlocal/en-gb/this-does-not-exist -> should serve index.html)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
</IfModule>
我的虚拟主机
<VirtualHost 192.168.56.1:80>
DocumentRoot "C:\Users\Dev\Documents\angular-10\dist"
ServerName angular.wlocal
ServerAlias angular.wlocal
RemoteIPHeader X-Forwarded-For
<Directory "C:\Users\Dev\Documents\angular-10\dist">
DirectoryIndex index.html
AllowOverride ALL
Require all granted
</Directory>
</VirtualHost>
如果我删除下面,网址会改变,但我会得到The requested URL /en-gb/ was not found on this server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
目前,该 URL 没有更改为 /en-gb - 我认为这是因为 RewriteRule . index.html [L] 删除了它。
如何在保留此 /en-gb 重写的同时从根目录提供 index.html?
【问题讨论】:
-
"这也应该适用于目录。例如。
http://angular.wlocal/css/style.css" - nitpick,但这是一个文件,而不是目录。大概您的内部链接已经包含此en-gb国家/语言代码?
标签: apache .htaccess mod-rewrite