使用htaccess,我想将http强制为https,同时将其重定向到www.并转到我的应用程序文件所在的公用"文件夹. 结果应该是example应该重定向到www.example/public/
Using htaccess I want to force http to https and at the same time redirect to www. and to the 'public' folder where my application files are. The result should be that example should redirect to www.example/public/
要重定向到https和www,我可以使用:
To redirect to https and www I manage by using:
RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ www.%1%{REQUEST_URI} [L,NE,R=301]使用以下方法重定向到我管理的公共"文件夹:
To redirect to the 'public' folder I manage by using:
ReWriteCond %{REQUEST_URI} !^public/ ReWriteRule ^(.*)$ public/$1 [L]现在,我需要将它们结合起来,但是我的多次尝试并没有导致有效的重定向,因此我需要一些建议.
Now I need to combine these but my many attempts did not result in a working redirect and I would need some advise on this.
在文件夹public中是另一个htaccess文件:
In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production" RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ /index.php [NC,L]<<
推荐答案ReWriteCond %{REQUEST_URI} !^public/ ReWriteRule ^(.*)$ public/$1 [L]
REQUEST_URI服务器变量以斜杠开头,因此上述条件将永远不匹配.例如:
The REQUEST_URI server variable starts with a slash, so the condition above will never match. For example:
RewriteCond %{REQUEST_URI} !^/public/ RewriteRule (.*) public/$1 [L]但是,假设您在/public子目录中还有另一个.htaccess文件,我将像下面这样编写该块:
However, assuming you have another .htaccess file in the /public subdirectory I would write this block like the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule (.*) public/$1 [L]这样做还有一个好处,就是可以在URL的开头(如果需要)允许public路径段.例如. URL /public/foo会在内部重写为/public/public/foo,而不会创建重写循环.
This has the added benefit of allowing a public path segment at the start of your URLs (if you wish). eg. The URL /public/foo would internally rewrite to /public/public/foo without creating a rewrite loop.
RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ www.%1%{REQUEST_URI} [L,NE,R=301]
在第一种情况下,您缺少OR标志.例如:
You are missing an OR flag on the first condition. For example:
RewriteCond %{HTTPS} off [OR] :否则,它将无法将www.example/重定向到HTTPS.
Without that, it will fail to redirect www.example/ to HTTPS.
请澄清一下,规范重定向应该在内部重写到public目录之前进行.
Just to clarify, the canonical redirect should go before the internal rewrite to the public directory.
更新:
在公用文件夹中是另一个htaccess文件:
In the folder public is another htaccess file: SetEnv APPLICATION_ENV "production" RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ /index.php [NC,L]
最后一个RewriteRule指令上的 substitution 字符串不正确.您需要从/index.php中删除斜杠前缀.它应该只读取index.php.否则,它将把请求改写回文档根目录(文件系统路径),并导致无限循环或404(取决于父指令).通过将其更改为index.php(无斜杠),它现在相对于/public目录(包含.htaccess文件的目录),而不是文档根目录.
The substitution string on the last RewriteRule directive is incorrect. You need to remove the slash prefix from /index.php. It should just read index.php. Otherwise it is going to rewrite the request back to the document root (filesystem path) and result in either an endless loop or a 404 (depending on the parent directives). By changing it to index.php (no slash) it is now relative to the /public directory (the directory that contains the .htaccess file), instead of the document root.
您还可以简化RewriteRule 模式,此处不需要NC标志.因此,总而言之,/public/.htaccess文件应显示为:
You can also simplify the RewriteRule pattern and the NC flag is not required here. So, in summary, the /public/.htaccess file should read:
SetEnv APPLICATION_ENV "production" RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^ index.php [L]更多推荐
在htaccess中重定向到文件夹和https
发布评论