Skip to content

Lightsail WordPress Instance - Incorrect HTTP to HTTPS Redirect (302 instead of 301)

0

My Lightsail WordPress instance is issuing a 302 (temporary) redirect from HTTP to HTTPS instead of the desired 301 (permanent) redirect. I believe 302 is not ideal for SEO. I have already ruled out. htaccess and WordPress plugins as the cause.

  1. I have confirmed the following:
  2. The issue occurs even when accessing a plain HTML file, indicating it's not related to WordPress.
  3. My.htaccess file does not contain any redirect rules.
  4. I have disabled all caching plugins.
  5. I have verified that there are no load balancers or distributions associated with my instance.
  6. I have checked the Bitnami configuration files (including /opt/bitnami/apache/conf/bitnami/bitnami.conf and /opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf) and found no rules that would cause this redirect.

can anyone help me whats causing the issue why its redirecting to 302 only instead of 301. Any help would be appreciated thank you

asked 9 months ago268 views
2 Answers
1
Accepted Answer

Hello.

Please change "bitnami.conf" as follows.
"[R,L]", which is set by default when setting an SSL certificate, uses 302 as the HTTP status code.
Therefore, you can set the status code to 301 by setting "[R=301,L]".

# Default Virtual Host configuration.

# Let Apache know we're behind a SSL reverse proxy
SetEnvIf X-Forwarded-Proto https HTTPS=on

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache/htdocs"
  # BEGIN: Configuration for letsencrypt
  Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
  # END: Configuration for letsencrypt
  # BEGIN: Support domain renewal when using mod_proxy without Location
  <IfModule mod_proxy.c>
    ProxyPass /.well-known !
  </IfModule>
  # END: Support domain renewal when using mod_proxy without Location
  # BEGIN: Enable HTTP to HTTPS redirection
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R=301,L] #edit
  # END: Enable HTTP to HTTPS redirection
  <Directory "/opt/bitnami/apache/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html
  # BEGIN: Support domain renewal when using mod_proxy within Location
  <Location /.well-known>
    <IfModule mod_proxy.c>
      ProxyPass !
    </IfModule>
  </Location>
  # END: Support domain renewal when using mod_proxy within Location
</VirtualHost>
Include "/opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf"

Also, change "/opt/bitnami/apache/conf/vhosts/wordpress-vhost.conf" as follows.

<VirtualHost 127.0.0.1:80 _default_:80>
  ServerName www.example.com
  ServerAlias *
  DocumentRoot /opt/bitnami/wordpress
  # BEGIN: Configuration for letsencrypt
  Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
  # END: Configuration for letsencrypt
  # BEGIN: Support domain renewal when using mod_proxy without Location
  <IfModule mod_proxy.c>
    ProxyPass /.well-known !
  </IfModule>
  # END: Support domain renewal when using mod_proxy without Location
  # BEGIN: Enable HTTP to HTTPS redirection
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R=301,L] #edit
  # END: Enable HTTP to HTTPS redirection
  <Directory "/opt/bitnami/wordpress">
    Options -Indexes +FollowSymLinks -MultiViews
    AllowOverride None
    Require all granted
    # BEGIN WordPress fix for plugins and themes
    # Certain WordPress plugins and themes do not properly link to PHP files because of sy
mbolic links
    # https://github.com/bitnami/bitnami-docker-wordpress-nginx/issues/43
    RewriteEngine On
    RewriteRule ^bitnami/wordpress(/.*) $1 [L]
    # END WordPress fix for plugins and themes
    # BEGIN WordPress
    # https://wordpress.org/support/article/htaccess/#basic-wp
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    # BEGIN Disable WordPress XML-RPC endpoint
    # Disable the outdated WordPress XML-RPC endpoint to prevent security vulnerabilities.
    # https://github.com/bitnami/containers/pull/51077
    <Files xmlrpc.php>
    Order Allow,Deny
    Deny from all
    </Files>
    # END Disable WordPress XML-RPC endpoint
  </Directory>
  Include "/opt/bitnami/apache/conf/vhosts/htaccess/wordpress-htaccess.conf"
  # BEGIN: Support domain renewal when using mod_proxy within Location
  <Location /.well-known>
    <IfModule mod_proxy.c>
      ProxyPass !
    </IfModule>
  </Location>
  # END: Support domain renewal when using mod_proxy within Location
</VirtualHost>

After changing the configuration file, stop and start it using the following commands.

sudo /opt/bitnami/ctlscript.sh stop
sudo /opt/bitnami/ctlscript.sh start
EXPERT
answered 9 months ago
EXPERT
reviewed 9 months ago
  • Bro you are the best. Thanks a lot it works like wonders

0

The issue you're experiencing with your Lightsail WordPress instance issuing a 302 (temporary) redirect instead of a 301 (permanent) redirect from HTTP to HTTPS could be related to the Apache configuration on your Bitnami WordPress stack.

Given that you've already checked the common causes and ruled out WordPress plugins and .htaccess files, the problem likely lies in the Apache configuration files.

To resolve this issue, you might need to modify the Apache configuration. Specifically, you should look into editing the file located at /opt/bitnami/apache/conf/vhosts/00_status-vhost.conf. In this file, you can add a rewrite rule to force a 301 redirect from HTTP to HTTPS.

Here's an example of what you could add to that file:

Define CUSTOM_DOMAIN_NAME "yourdomain.com"
RewriteEngine On
<VirtualHost *:80 *:443>
    ServerName www.yourdomain.com
    RewriteRule ^/(.*) https://${CUSTOM_DOMAIN_NAME}/$1 [R=301,L]
</VirtualHost>

Replace "yourdomain.com" with your actual domain name. This configuration tells Apache to use a 301 (permanent) redirect for all HTTP requests to the HTTPS version of your site.

After making these changes, you'll need to restart the Apache service for the changes to take effect. You can do this by running the appropriate restart command for your Bitnami stack.

If this doesn't resolve the issue, you may need to investigate further into other Apache configuration files or consult with AWS support for more specific guidance related to your Lightsail instance configuration.
Sources
Lightsail Bitnami Wordpress with CloudFront www. Redirect | AWS re:Post

answered 9 months ago
EXPERT
reviewed 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.