SNo*_*Nos 9 .htaccess mod-rewrite redirect
我试图强制我的根域的子文件夹(/ bbb /)始终显示为https.我的.htaccess文件也处理页面的扩展.
我把.htaccess文件放在我的/bbb/文件夹中,但当我试图强制连接到https时,我得到"太多的重定向",没有它一切正常.
我的代码有什么问题?
Options +FollowSymLinks -MultiViews
Options +Indexes
AcceptPathInfo Off
RewriteEngine on
RewriteBase /
ErrorDocument 404 https://example.co.uk/404page/404.html
#Force from http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]
#take off index.html
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
Run Code Online (Sandbox Code Playgroud)
har*_*are 37
试试这个:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
har*_*are 20
如果您有代理服务器或使用共享主机,那么有时您选择通过CloudFlare获得免费SSL.如果你使用的是像codeigniter或laravel这样的框架,那么你总是有一个路由文件.所以有时我上面给出的答案可能不起作用.
在这种情况下,当您尝试重定向到htpps时,您可能会获得无限循环.所以要解决你可以尝试下面:
RewriteEngine On
# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
问题出在这条规则上:
#Force from http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]
Run Code Online (Sandbox Code Playgroud)
将此规则更改为:
#Force from http to https
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} =bbb.example.co.uk
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,NE]
Run Code Online (Sandbox Code Playgroud)
由于在开始时使用,您的条件被逆转!,并且在结束时有一个额外的斜杠,它永远不会匹配,因此使您的条件始终返回 true。
在测试之前请确保清除浏览器缓存。
.htaccess 文件中的重定向
.htaccess 文件是一个配置文件,用于修改网站/服务器上每个目录的 Apache 服务器行为。这是一个用户级配置文件,尽管重定向很常见,但这里只能编辑一些 Apache 配置。
您可以拥有多个级联在一系列目录中的 .htaccess 文件。如果父目录中有一个 .htaccess,子目录中有另一个 .htaccess,它们都会影响子目录。在这些情况下,重要的是要记住您在哪里有和没有 .htaccess 文件,以防止不同级别的 .htaccess 文件之间发生冲突。
以下是一系列重定向示例,将有助于识别 .htaccess 文件中的重定向。这些并不是执行此类重定向的唯一方法,但这些应该向您展示最常见的重定向,以便您可以识别它们(如果它们位于您正在使用的 .htaccess 文件中)。
强制 HTTPS 下面的 .htaccess 代码首先检查请求是使用 HTTP 还是 HTTPS 进入服务器。如果请求未使用 HTTPS,则配置将告诉浏览器重定向到之前请求的同一网站和 URL 的 HTTPS 版本。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)
强制 HTTPS:在负载均衡器或代理(CloudFlare/Incapsula/Sucuri/等)后面时有时您可能会使用代理,例如负载均衡器或 Web 防火墙,例如 CloudFlare、Incapsula 或 Sucuri。这些可以配置为在前端使用 SSL,但在后端不使用 SSL。为了使其正常工作,您不仅需要检查请求中的 HTTPS,还需要检查代理是否仅使用 HTTP 将原始 HTTPS 请求传递到服务器。以下规则检查请求是否是从 HTTPS 转发的,如果是,则不会尝试再次重定向。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)
强制非 www 此重定向仅检查请求的网站名称是否以域名开头的 www 开头。如果包含 www,它会重写请求并告诉浏览器重定向到非 www 版本的域名。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)
强制 www 最后一个重定向检查是否未在域名开头以 www 请求网站名称。如果不包含 www,它会重写请求并告诉浏览器重定向到该域的 www 版本。
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)