htaccess https 重定向仅针对特定的 Zend Frameworks 控制器/动作 [英] htaccess https redirect only on specific Zend Frameworks controller/actions

查看:23
本文介绍了htaccess https 重定向仅针对特定的 Zend Frameworks 控制器/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个社区的新手,但我不时发现它真的很有用.

I'm new to this community, but I've found it really useful time to time.

我已经搜索了很多答案,但没有找到我需要的任何东西.我试图自己解决它,但我仍然遇到错误,所以我希望找到可以向我展示正确方法的人...... :-)

I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)

我有一个经典"的 ZF 网站,其中有许多控制器/操作 URL,这些 URL 使用 .htaccess 文件重定向到 index.php.

I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.

现在,我需要的是将几个控制器重定向到 https ssl 连接,不包括两个控制器的某些操作.

Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.

我尝试这样做的方式是:

The way I was trying to do it is:


Options +FollowSymLinks

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^mydomain.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php

当我去/member 控制器时它似乎工作但是,当我转到另一个控制器时,例如/index 或/about 它不会重定向到 http 连接(端口 80),并且如果我尝试稍微更改重写条件正则表达式,它有时会执行重定向循环,并且浏览器给我一条通知,阻止与该网站的连接.

It seems to work when I go to /member controller But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.

有没有人可以向我展示在我的重写条件中使用的正确合成器,以允许两个控制器(不包括给定的操作)在 https 连接下并在更改控制器时返回到标准的 http 连接?

Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?

提前致谢.

亚历山德罗

推荐答案

我终于解决了!我坚持寻找答案,因为我认为用 .htaccess 做它更干净、更智能且易于维护.问题主要是由于ssl to non-ssl"块中使用的正则表达式没有正确匹配传递的值(现在读取环境变量 %{THE_REQUEST} 时最匹配,避免在某些情况下出现错误重定向循环.

Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain. The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.

我在这里粘贴工作代码以供进一步参考:

I paste here the working code for further reference:


    Options +FollowSymLinks

    RewriteEngine On

    RewriteBase /

    RewriteOptions MaxRedirects=1



    RewriteCond %{HTTP_HOST} ^yoursite.tld [NC]

    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]



    RewriteCond %{HTTPS} off

    RewriteCond %{THE_REQUEST} ^[A-Z]+ /((controller1|controller2)/(?!(action1|action2))(.*)) HTTP/

    RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]



    RewriteCond %{HTTPS} on

    RewriteCond %{THE_REQUEST} !^[A-Z]+ /((controller1|controller2)/(?!(action1|action2))(.*)) HTTP/

    RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]



    RewriteCond %{REQUEST_FILENAME} .(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]

    RewriteCond %{REQUEST_FILENAME} -s [OR]

    RewriteCond %{REQUEST_FILENAME} -l [OR]

    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^.*$ - [NC,L]



    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^.*$ index.php [NC,L]

这篇关于htaccess https 重定向仅针对特定的 Zend Frameworks 控制器/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆