大网数据广告活动
网站运营与故障问答

How to redirect all url to https except one?

· 52CE 我爱测

# Go to https if not on firmware RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} !^/firmware$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] # Go to http if you are on firmware RewriteCond %{SERVER_PORT} !80 Re…

问题原文

原帖链接 · Abdus Sattar Bhuiyan · CC BY-SA 4.0

# Go to https if not on firmware
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/firmware$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# Go to http if you are on firmware
RewriteCond %{SERVER_PORT} !80 
RewriteCond %{REQUEST_URI} ^/firmware$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

This code should redirect /firmwire to http instead of https. But example.com/firmware redirect to example.com/index.php. whats problem is going on?

The code is run on apache server and drupal 7. Any idea?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

# Go to http if you are on firmware
RewriteCond %{SERVER_PORT} !80 
RewriteCond %{REQUEST_URI} ^/firmware$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

This code should redirect /firmwire to http instead of https.

Both your code blocks redirect to HTTPS. In fact, you would expect the above to trigger a redirect loop since it is potentially redirecting to itself. That is unless /firmware is a physical filesystem directory - or it's not actually being processed at all?

But example.com/firmware redirect to example.com/index.php

This suggests you have a conflict with existing directives - perhaps your redirects are in the wrong place? The order of mod_rewrite directives is important.

These directives need to go near the top of your .htaccess before the Drupal front-controller.

These directives can also be tidied a bit; no need for the additional condition that checks the URL-path. This check should be performed by the RewriteRule pattern. For example:

# Go to https if not on firmware
RewriteCond %{SERVER_PORT} 80 
RewriteRule !^firmware$ https://www.example.com%{REQUEST_URI} [NC,R,L]

# Go to http if you are on firmware
RewriteCond %{SERVER_PORT} !80 
RewriteRule ^(firmware)$ http://www.example.com/$1 [NC,R,L]

The NC flag is only required if /firmware really can be mixed case.

If /firmware is a physical filesystem directory then this may need modifying (as mod_dir will, by default, append a trailing slash).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。