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

HTTP website redirect all except 1 file to https

· 52CE 我爱测

I have a website where I'm wanting 1 file (version.txt) to be accessible via regular HTTP, but I want everything else to redirect to https. To do this, I added the "RedirectMatch" line below in my /etc/httpd/conf.d/somewhere.com.…

问题原文

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

I have a website where I'm wanting 1 file (version.txt) to be accessible via regular HTTP, but I want everything else to redirect to https. To do this, I added the "RedirectMatch" line below in my /etc/httpd/conf.d/somewhere.com.conf file (running centos7). Before I wanted to serve this file via HTTP, the line was a "Redirect permanent" type line. When I first made this change, I did some tests and it appeared to work; I was definitely able to access version.txt with plain HTTP, and I thought other accesses were being redirected to https. However, I just navigated to the website today and noticed it was all coming over HTTP. I have very little knowledge with websites, so I'm guessing that my testing for the redirect was faulty due to browser caching or something that tricked me into thinking it was working.

The question: in my conf.d file below, why doesn't the website redirect everything except version.txt to https? How can I change it so that it allows HTTP access of version.txt, but redirects everything else to HTTPS?

(bonus if you have any ideas on what fooled me into thinking it was working at first)

<VirtualHost *:80>
  ServerName somewhere.com
  ServerAdmin [email omitted]
  DocumentRoot /var/www/somewhere.com
  ErrorLog /etc/httpd/logs/error_log_somewhere
  CustomLog /etc/httpd/logs/access_log_somewhere combined
  RedirectMatch permanent "^(/*version.txt/.*)" https://somewhere.com$1
</VirtualHost>
<VirtualHost *:443>
  SSLEngine on
  SSLCertificateFile /etc/pki/tls/certs/somewhere.com_ssl_certificate.cer
  SSLCertificateKeyFile /etc/pki/tls/private/_.somewhere.com_private_key.key
  SSLCertificateChainFile /etc/pki/tls/certs/_.somewhere.com_ssl_certificate_INTERMEDIATE.cer
  ServerName somewhere.com
  ServerAlias www.somewhere.com
  ServerAdmin [email omitted]
  DocumentRoot /var/www/somewhere.com
  ErrorLog /etc/httpd/logs/error_log_somewheres
  CustomLog /etc/httpd/logs/access_log_somewheres combined
</VirtualHost>
ServerSignature Off
ServerTokens Prod
Header always append X-Frame-Options SAMEORIGIN

已采纳解答

原帖链接 · Wayne Smith · CC BY-SA 4.0

If it were me; I would set up to give the http its own directory and redirect anything that would be a 404 to the https using the .htaccess of http location. This keeps things easy to understand for any future revisits to how the site is working, and uses coding that is well established and followed by anyone else who may need to work on the site.

A a2enmod rewrite (apache2) may be required if you are self hosting or VPS hosting to enable rewrite mod; but it appears to be already enable? in your case.

The .conf you are using

<VirtualHost *:80>
  ServerName somewhere.com
  ServerAdmin [email omitted]
  DocumentRoot /var/www/port80-somewhere.com
  ErrorLog /etc/httpd/logs/error_log_somewhere
  CustomLog /etc/httpd/logs/access_log_somewhere combined
</VirtualHost>
# allow .htaccess over ride may already be allowed.
<Directory /var/www/port80-somewhere.com/>
    Options -Indexes FollowSymLinks
    AllowOverride all
    Require all granted
</Directory>

CentOS 7 may want to AllowOverride here /etc/httpd/conf/httpd.conf But I think it is already allowed.

<Directory /var/www/port80-somewhere.com/>
    Options -Indexes FollowSymLinks
    AllowOverride all
    Require all granted
</Directory>

/var/www/port80-somewhere.com/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]

Thus; if version.txt exists in /var/www/port80-somewhere.com/ it will be presented from port 80. Anything that does not exist in /var/www/port80-somewhere.com/ is 301ed to the secure site. So you can pick and choose at will what will be available on http without needing to reconfigure.

Note: I would also put the favicon.ico in /var/www/port80/favicon.ico because the browser will ask for it.

Update

The .htaccess is a derivative of Apache documentation, which is in common usage by Wordpress sites.

# Apache configuration file
# http://httpd.apache.org/docs/2.2/mod/quickreference.html

# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>

# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Except as you noted does not redirect the root of the domain itself. Sorry I missed that.

  • First condition... the file "-f" requested does not "!" exist.
  • Second condition ... the request is not "!" a directory "-d"
  • Third condition ... we don't want the root index.php or index.html either. If the request URI is HTTP://example.com/ the requested URI is "/" or ^/$ as the condition.

The one you are using per comments is good. My thought of using the %{REQUEST_URI} is that if revisited in the future the only thing that may need to be changed is the site within "". So less things to go wrong.

If conditions are true 301 to new location.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]

We need to use the rewrite engine. Depending on the build if one is using the installed version of apache from the linux os. It may not be turned on? Apache came to the rescue with a2enmod ... which allows it to be turned on in a way that will not break on a future release, (thank you Apache).

The virtual host looks fairly off the shelf so it will not break in future releases. But we need to allow .htaccess so we can configure directories to rewrite etc using .htaccess so that we don't need to restart the server. The derivative of /etc/apache2/sites-available/000-default.conf use AllowOverride all.

<Directory /var/www/port80-somewhere.com/>
    Options -Indexes FollowSymLinks
    AllowOverride all
    Require all granted
</Directory>

Specifically in the directory where HTTP exists. We want .htaccess enabled. "AllowOverrided all" does this; and "Require all granted" is the newer syntax of "Allow from all" which relates to blocking IP addresses from access to the host, don't want to block anyone, "all granted"; The -Indexes is turning off listing of the files in a directory, there is not one but we don't want to need to change this if usage changes.

All these might be turned on by some builds but if we need to move the site, nice if we can do that without issues.

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

资料来源与适用范围

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

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

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