【发布时间】:2016-11-05 20:02:35
【问题描述】:
我的项目的 Web 根目录中有以下 .htaccess 文件:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
以上工作。
当我导航到:
http://localhost/repos/eamorr.co.uk/react_www
我看到了我的主页。
当我导航到:
http://localhost/repos/eamorr.co.uk/react_www/contact-us
我看到我主页的“联系我们”路线(我的网页是用 React 编写的)。
好的。太好了。
现在,部分用户使用 Linux:
当他们签出代码时,他们看到的网站网址略有不同:
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/
和
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/contact-us
关于.htaccess文件的这一行:
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
除了. 之外,我可以输入一些条件来从不同的路径提供 index.html 吗?
例如
RewriteRule {{condition1???}} /repos/eamorr.co.uk/react_www/index.html [L,QSA]
或(不同的条件=不同的路径):
RewriteRule {{condition1???}} /_eamorr/develop/eamorr.co.uk/react_www/index.html [L,QSA]
恐怕我的 Apache 知识跟不上,无法快速完成这项工作,我已经花了好几个小时!
==================
解决方案:
使用级联 htaccess 重写规则设置如下:
RewriteEngine On
#RewriteBase /
#Linux localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*repos/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /repos/eamorr.co.uk/react_www/index.html [L,QSA]
#Mac localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*_eamorr/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /_eamorr/eamorr.co.uk/react_www/index.html [L,QSA]
#develop .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/develop/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/develop/react_www/index.html [L,QSA]
#master .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/master/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/master/react_www/index.html [L,QSA]
#default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L,QSA]
这足以满足我现在的需求。
【问题讨论】:
标签: apache mod-rewrite