Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache Https到http,存在各种异常_Apache_.htaccess_Https_Url Redirection_Http Redirect - Fatal编程技术网

Apache Https到http,存在各种异常

Apache Https到http,存在各种异常,apache,.htaccess,https,url-redirection,http-redirect,Apache,.htaccess,Https,Url Redirection,Http Redirect,我们的网站将很快切换到SSL,但我们还没有准备好。因此,我们将在htaccess中将所有https URL重定向到http。我已经有工作,但作为一个额外的复杂性,我们有一些网址,必须是例外重定向。这些URL是: someone.dev.www.example.com example.com/top\u deal example.com/watchs 我的重定向是这样工作的: RewriteCond%{HTTP:X-Forwarded-Proto}=https 重写规则^(.*)$http://%

我们的网站将很快切换到SSL,但我们还没有准备好。因此,我们将在htaccess中将所有https URL重定向到http。我已经有工作,但作为一个额外的复杂性,我们有一些网址,必须是例外重定向。这些URL是:
someone.dev.www.example.com
example.com/top\u deal
example.com/watchs

我的重定向是这样工作的:
RewriteCond%{HTTP:X-Forwarded-Proto}=https
重写规则^(.*)$http://%{http\u HOST}%{REQUEST\u URI}[L,R=302]


但是不能让异常工作。我已经尝试了我能在网上找到的所有解决方案。

首先,您希望它是临时重写(=302)还是浏览器应该将其保存为永久性
,从而获得
301
?只是问一下,这两种方法都有效,但如果你想暂时使用,你应该阅读

摘录:HTTP/1.1(RFC 2616)添加了新的状态代码303和307

因此,在您的情况下,使用
307
可能会更好。

如果要进行永久重写,请使用
[R=301]
使浏览器缓存重写目标,而不是每次重写


现在来看看错误的解决方案(但是我留在那里有一个好的原因,请看)不要使用它

#logic is: 1 OR (2 AND 3) --> NO, it isn't, see the link right above!
#1 being hostname: somebody.dev.www.example.com
#2 being hostname: example.com
#3 being REQUEST_URI = !^/(top_deal|watches)
RewriteCond %{HTTP_HOST} ^somebody.dev.www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} !^/(top_deal|watches)$
RewriteRule .? - [END]
##[END] makes the rewrite-evaluation STOP, completely, not only for this turn!

#same rule as before, just changed 302 -> 307 (RFC 2616) and ^(.*)$ to .? cuz I like it better^^
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [L,R=307]

更新

以下是您的配置中真正应该包含的内容:

# Don't rewrite HOST 'somebody.dev.www.example.com'
RewriteCond %{HTTP_HOST} ^somebody.dev.www.example.com$
RewriteRule ^ - [END]

# Don't rewrite HOST 'example.com', if URI 'top_deal' OR 'watches'
RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} ^/(top_deal|watches)$
RewriteRule ^ - [END]

RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=307]
这肯定能完成任务:)

如果没有,出于某种原因,搜索您的
LogLevel
,并将
rewrite:traceX
添加到该行。因此,如果您的
LogLevel
error
,则应如下所示:

LogLevel error rewrite:trace2

然后重新启动/重新加载服务器并再次测试。Error.log现在将显示尝试重写的结果。使用
tail-f error_log | fgrep'[重写:'
查找它们。如果它们不清楚,请告诉我们,也许我们可以提供帮助:)

您可以将规则用作第一条规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} !=somebody.dev.www.example.com
RewriteCond %{THE_REQUEST} !\s/+(?:top_deal|watches)/?[?\s] [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# rest of your rules go here

在测试之前,请确保清除浏览器缓存。

我最终完成的工作如下:

RewriteCond %{HTTP_HOST} !^(.*).dev.www.example.com
RewriteCond %{REQUEST_FILENAME} ! top_deal(.*)
RewriteCond %{REQUEST_FILENAME} ! watches(.*)
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是,如果有其他子域不是
example.com
,但也使用URI
/watchs
top\u deal
?我只想在一个规则中执行此操作。如果您编写的是一个可能性,则需要将其分解为两个单独的规则。这不起作用。直接转到https。根本没有重定向。这是什么您在浏览器中输入的准确的
http
URL被重定向到
https
?Ddi您是否清除了浏览器缓存?这不起作用。我收到500个内部服务器错误…这比我一天半没有收到的要好。添加了一些解释,正确的答案,希望能为进一步的疑难解答提供一些帮助叮叮声