我需要为angular2配置虚拟主机。我试过这篇文章
https://www.packtpub.com/mapt/book/Web+Development/9781783983582/2/ch02lvl1sec15/Configuring+Apache+for+Angular
根据这个我需要像这样设置虚拟主机
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
任何人都可以告诉我应用程序的路径是什么,因为我的应用程序在默认的角度2端口4200上运行。 还有其他办法吗。
答案 0 :(得分:15)
angular-cli build
在本地开发环境中,在项目根目录中运行ng build --prod
。
这将创建一个名为dist
的文件夹,您希望将dist
中的所有文件和文件夹放到服务器上的Apache根目录中。
设置apache以提供到index.html的路由。您可以使用两种方法,编辑虚拟主机或在网站根目录中使用.htaccess。
选项1:虚拟主机
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
选项2:.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>