我的 React 应用程序在本地开发服务器上运行良好,但当我将生产就绪文件直接转储到 Apache 的 htdocs 目录时,它不起作用:
这是我所拥有的:
/var/www/index.html
/var/www/bundle.js
我有
DocumentRoot /var/www
在 /etc/apache2/sites-available/000-default.conf 中
事实是
1)。当我访问时http://...com/这将我带到了登录页面
2)。我点击链接后
<Link to="main"><button>Log In</button></Link>
浏览器位置字段中的内容变为:
http://...com/main
3)。现在如果我重新加载这个网址(http://...com/main), I got
The requested URL /main was not found on this server
我在 React 中的路由:
<Router history={browserHistory }>
<Route path="/" component={TopContainer}>
<IndexRoute component={Login} />
<Route path='main' component={MainContainer} />
</Route>
</Router>
apache 配置中还缺少什么?
thanks
改变虚拟主机配置(通常发现于/etc/httpd/conf.d\vhosts.conf
)通过添加以下 Rewrite* 行:
<VirtualHost *:8080>
ServerName example.com
DocumentRoot /var/www/httpd/example.com
<Directory "/var/www/httpd/example.com">
...
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>
这告诉 Apache 提供任何存在的文件,但如果它们不存在,则只提供服务/index.html
而不是一个404: not found
.
-
阿帕奇参考:配置 Apache 虚拟主机
-
react-router
历史参考:配置您的服务器
感谢盗取的完整答案here
编辑:在当前 apache 版本中“On”需要大写
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)