htaccess https 仅在特定 Zend Frameworks 控制器/操作上重定向 [英] htaccess https redirect only on specific Zend Frameworks controller/actions

查看:29
本文介绍了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,它们通过 .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"块中使用的正则表达式没有正确匹配传递的值(现在最好匹配读取env变量%{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天全站免登陆