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

Using htaccess to force HTTPS doesn't work

· 52CE 我爱测

NEW EDIT 28/oct/19 → I edited the .htaccess file as you said AddHandler x-mapp-php5.5 .php # HTTP to HTTPS redirect RewriteCond %{SERVER_PORT} 80 RewriteRule (.*) https://www.bebechan.net/$1 [R=301,L] # BEGIN WordPress <IfModule …

问题原文

原帖链接 · べべちゃんBebechan · CC BY-SA 4.0

NEW EDIT 28/oct/19 → I edited the .htaccess file as you said


AddHandler x-mapp-php5.5  .php

# HTTP to HTTPS redirect
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.bebechan.net/$1 [R=301,L]

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

# END WordPress

I also added the lines in wp.config :

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'mIQnU';


define('ALLOW_UNFILTERED_UPLOADS', true);
define('FORCE_SSL', true);
define('FORCE_SSL_ADMIN',true);

/* That's all, stop editing! Happy publishing. */

I checked the wp settings and my addresses are well written (https://bebechan.net). I even downloaded a plugin to redirect every http:/bebechan.net/ebook to https://bebechan.net/ebook.

STILL NOT WORKING sadly : (. Every time I go to bebechan.net/ebook, it's not https but http.

Something I wanna point out is that, even when I go directly to the https://bebechan.net/ebook, I can access the 'buy' page, BUT, the little green lock doesn't appear, I checked the 'why no padlock' website (https://www.whynopadlock.com/results/cf25cdd8-0cde-4cfb-bfc6-1dc0ac4e2e12) and it's written that 'You currently have TLSv1 enabled'. Might this be part of the problem? Should I fix it?


BIG EDIT: The host that takes care of my SSL just told me ''The SSL that we provide with your contract only protect one domain OR one subdomain, not both''. They told them then that it's why bebechan.net/ebook wasn't protected but bebechan.net was. But, bebechan.net/ebook isn't a subdomain right? It's still part of my domain bebechan.net? Or am I totally wrong? A bit lost here.

Thanks!

I saw similar questions but they didn't resolve my issue.

I have an active SSL on my WordPress website (hosted by IONOS 1and1). If I enter my example.com it goes to HTTPS. If I enter example.com/smth it goes to HTTP (without s), if I enter www.example.com/smth if goes to HTTPS!

So my wish is to force HTTPS on example.com/smth. I created a .htaccess file through Filezilla entering all the code I could find for example :

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

or

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]

I tried it with the wordpress addition:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

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

But nothing seems to redirect! (The page is bebechan.net/ebook). Maybe I've done something wrong creating the .htacces or something?

EDIT: Actually, I just checked right now and it's written that the SSL is provided for clickandbuild/bebechan and I indeed have a clickandbuild folder when I check my FTP (see screenshot below) and there is already a .htaccess there. Maybe that's the issue? Maybe I should edit this one instead of creating a new one in the root?

So Actually there is already an .htaccess in the folder .../clickandbuild/bebechan which might be where my SSL is. Looks like there is already a https forced written here. Still not working tho. I'm super lost haha.

Content of the .htaccess:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/cache-manifest
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/vcard
    AddOutputFilterByType DEFLATE text/vnd.rim.location.xloc
    AddOutputFilterByType DEFLATE text/vtt
    AddOutputFilterByType DEFLATE text/x-component
    AddOutputFilterByType DEFLATE text/x-cross-domain-policy
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/ld+json
    AddOutputFilterByType DEFLATE application/atom+xml
    AddOutputFilterByType DEFLATE application/manifest+json
    AddOutputFilterByType DEFLATE application/rdf+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/schema+json
    AddOutputFilterByType DEFLATE application/vnd.geo+json
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/x-web-app-manifest+json
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE font/eot
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE image/bmp
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/vnd.microsoft.icon
    AddOutputFilterByType DEFLATE image/x-icon
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css A2419200
    ExpiresByType text/x-component A2419200
    ExpiresByType application/x-javascript A2419200
    ExpiresByType application/javascript A2419200
    ExpiresByType text/javascript A2419200
    ExpiresByType text/x-js A2419200
    ExpiresByType text/html A3600
    ExpiresByType text/richtext A3600
    ExpiresByType image/svg+xml A3600
    ExpiresByType text/plain A3600
    ExpiresByType text/xsd A3600
    ExpiresByType text/xsl A3600
    ExpiresByType text/xml A3600
    ExpiresByType video/asf A2419200
    ExpiresByType video/avi A2419200
    ExpiresByType image/bmp A2419200
    ExpiresByType application/java A2419200
    ExpiresByType video/divx A2419200
    ExpiresByType application/msword A2419200
    ExpiresByType application/vnd.ms-fontobject A2419200
    ExpiresByType application/x-msdownload A2419200
    ExpiresByType image/gif A2419200
    ExpiresByType application/x-gzip A2419200
    ExpiresByType image/x-icon A2419200
    ExpiresByType image/jpeg A2419200
    ExpiresByType application/json A2419200
    ExpiresByType application/vnd.ms-access A2419200
    ExpiresByType audio/midi A2419200
    ExpiresByType video/quicktime A2419200
    ExpiresByType audio/mpeg A2419200
    ExpiresByType video/mp4 A2419200
    ExpiresByType video/mpeg A2419200
    ExpiresByType application/vnd.ms-project A2419200
    ExpiresByType application/x-font-otf A2419200
    ExpiresByType application/vnd.ms-opentype A2419200
    ExpiresByType application/vnd.oasis.opendocument.database A2419200
    ExpiresByType application/vnd.oasis.opendocument.chart A2419200
    ExpiresByType application/vnd.oasis.opendocument.formula A2419200
    ExpiresByType application/vnd.oasis.opendocument.graphics A2419200
    ExpiresByType application/vnd.oasis.opendocument.presentation A2419200
    ExpiresByType application/vnd.oasis.opendocument.spreadsheet A2419200
    ExpiresByType application/vnd.oasis.opendocument.text A2419200
    ExpiresByType audio/ogg A2419200
    ExpiresByType application/pdf A2419200
    ExpiresByType image/png A2419200
    ExpiresByType application/vnd.ms-powerpoint A2419200
    ExpiresByType audio/x-realaudio A2419200
    ExpiresByType image/svg+xml A2419200
    ExpiresByType application/x-shockwave-flash A2419200
    ExpiresByType application/x-tar A2419200
    ExpiresByType image/tiff A2419200
    ExpiresByType application/x-font-ttf A2419200
    ExpiresByType application/vnd.ms-opentype A2419200
    ExpiresByType audio/wav A2419200
    ExpiresByType audio/wma A2419200
    ExpiresByType application/vnd.ms-write A2419200
    ExpiresByType application/font-woff A2419200
    ExpiresByType application/vnd.ms-excel A2419200
    ExpiresByType application/zip A2419200
</IfModule>

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

AddHandler x-mapp-php5.5  .php

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

# END WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.bebechan.net/$1 [R,L]
</IfModule> 

已采纳解答

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

tl;dr You're HTTP to HTTPS redirect is in the wrong place. It needs to go near the start of the /clickandbuild/bebechan/.htaccess file, not at the end. (See below for further explanation.)


There seems to be a lot of misinformation / confusion here...

The host that takes care of my SSL just told me "The SSL that we provide with your contract only protect one domain OR one subdomain, not both"

That should OK, you only appear to have one domain. However, your SSL cert does need to cover both the domain apex (ie. example.com) and the www subdomain (ie. www.example.com) - it should do - I would be very surprised if it didn't. And by what you stated at the start of the question; it does.

They told them then that it's why bebechan.net/ebook wasn't protected but bebechan.net was.

That's nonsense. The security certificate (SSL cert) protects hostnames, not subdirectories. If example.com is protected then example.com/ebook is also protected, as is example.com/<anything>.

But, bebechan.net/ebook isn't a subdomain right? It's still part of my domain bebechan.net?

Exactly.

I just checked right now and it's written that the SSL is provided for 'clickandbuild/bebechan' and I indeed have a clickandbuild folder when I check my FTP ... and there is already a .htaccess there. Maybe that's the issue? Maybe I should edit this one instead of creating a new one in the root?

It's possible that the document root is set to the directory /clickandbuild/bebechan, but as noted above, SSL certs cover hostnames, not subdirectories.

For the sake of the rest of this answer, I will assume that /clickandbuild/bebechan is actually the document root directory. ie. the directory that is served when you request http://example.com/ (HTTP and HTTPS).

Your FTP screenshot is confusing/ambiguous. If there is a .htaccess file in the /clickandbuild/bebechan subdirectory then presumably that is where WordPress is installed? However, your screenshot appears to show a .htaccess file and WordPress installation in the root directory?! I assume this is above the document root? So, what do these files relate to?

...there is already a .htaccess there. Maybe that's the issue? Maybe I should edit this one instead of creating a new one in the root?

This could very well be the issue. You should not need to manually create the .htaccess file. WordPress will create (and use) one by default - it is necessary to drive the WordPress "pretty" permalinks, which you appear to already be using.

So yes, edit the existing .htaccess file. Do not create another .htaccess file elsewhere.

If you create a .htaccess file in a parent directory (eg. in the root directory from your FTP screenshot - which I assume is where you are creating this .htaccess file?) with a mod_rewrite redirect (which is what this is) then it will likely be overridden by the mod_rewrite directives in the subdirectories .htaccess file and simply be ignored (which appears to be what is happening).

The end of your existing /clickandbuild/bebechan/.htaccess file reads...

:

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

AddHandler x-mapp-php5.5  .php

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

# END WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.bebechan.net/$1 [R,L]
</IfModule>

The main problem here is that the HTTP to HTTPS redirect is in the wrong place! It should not be at the end of the config file like this. It needs to go near the top of the .htaccess file - importantly, it needs to go before the WordPress front-controller (ie. the code block in the # BEGIN WordPress ... # END WordPress comment markers that currently precedes it).

By placing the redirect at the end, as it appears here then it will not do "much". It will redirect example.com/ (the document root), but not example.com/smth - which is the behaviour you stated initially in your question. However, you also stated that www.example.com/smth IS redirected - that behaviour cannot be explained by this.

In the above .htaccess file, the WordPress front-controller is also unnecessarily repeated - two blocks of code are identical - this is a mistake.

Solution

The end of that .htaccess file should read like the following instead:

:

AddHandler x-mapp-php5.5  .php

# HTTP to HTTPS redirect
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.bebechan.net/$1 [R=301,L]

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

# END WordPress

About wordpress, I'm not sure how is it installed and I havent configured wordpress to do anything. Maybe I should?

Yes, you should. Either using a plugin or manually... you need to ensure that the "WordPress Address (URL)" and "Site Address (URL)" are set correctly (ie. probably to https://www.example.com/). You may need to also set define('FORCE_SSL_ADMIN', true); in the wp-config.php file. There is more information about configuring WordPress elsewhere.

You don't want your .htaccess redirects to conflict with what WordPress thinks your canonical URL should be.

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

资料来源与适用范围

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

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

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