繁体   English   中英

如何将 http 重定向到 https 加上重定向`index.html` 到另一个 html 页面在 Z8052C47AB3B4ADCCC2A 一起?

[英]how to redirect http to https plus redirect `index.html` to another html page in .htaccess together?

I have a code in .htaccess that redirects http to https but I also want to add a code that redirects the index.html page to another html page. 有没有办法在.htaccess中同时做这两个。 下面提供的代码。

http 转 https 代码

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

index.html 到另一个 html 页面代码

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]
 RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^$ http://example.com/index.php [L,R=301]

是的,这是一个外部重定向,从http(s)://example.com/重定向到http://example.com/index.php However, you are redirecting to HTTP, not HTTPS and you are redirecting to index.php , whereas you stated index.html in your question. 似乎不需要检查请求的主机名,除非您有多个域并且只想重定向一个实例。

Unless you plan to implement HSTS then include this redirect before your general HTTP to HTTPS redirect and include the canonical hostname in your HTTP to HTTPS redirect.

例如:

RewriteEngine On

# Redirect / or /index.html TO /dir/index.html (HTTPS)
RewriteRule ^(index\.html)?$ https://example.com/dir/index.html [R=302,L]

# Redirect HTTP to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]

但是,在阅读了您的链接问题后,外部“重定向”可能不是您问题的正确解决方案。 当您应该链接到子目录中的文件时,听起来您可能“错误地”链接到文档根目录中的文件? (真的,如果是这种情况,您应该更正您的内部链接......)但是,一个可行的解决方案可能是在内部将请求重写为所需的 URL 路径,或者如果您要链接到文档根目录,则更改DirectoryIndex . 这对用户/客户端完全隐藏 - 用户只看到 URL A,但您实际上是在为 URL B 提供服务。但这取决于您网站的结构。

An external redirect, on the other hand, physically sends the user/browser to URL B. They see both URL A (the link they click on) and URL B (the URL they are being redirected to) - as does a search engine bot . 浏览器需要发出两个请求。 这对用户来说可能会更慢,并且在您的服务器上放置更多工作(不多,但仍然更多)。

你想先做你的 http --> https 因为这既适用于原始通信又适用于安全通信的重定向。

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    Header always set Content-Security-Policy "upgrade-insecure-requests;"

现在您可以将重定向应用到index.php

     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteRule ^index\.php$ - [L]
     RewriteCond %{REQUEST_FILENAME} -f
     RewriteCond %{REQUEST_FILENAME} -d
     RewriteRule . /index.php [L]
     </IfModule>

您可以调整-f-d以包含! 和/或更改文件名类型以满足您的特定需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM