One Page Zen

Web Design. Simplified.

  • Tutorials
    • Google Cloud
    • Amazon Cloud
    • Microsoft Cloud
  • Start
  • Resources

Configure 301 Redirects for Websites on Google Cloud (Apache)

November 12, 2020 Leron Amin 44 Comments

FacebookTweetLinkedInEmail

If you want to redirect webpages on your website to a new (permanent) URL, your best option is to configure a 301 redirect.

In this tutorial you will learn how to properly configure 301 redirects for websites running on Google Cloud Platform.

Before starting this tutorial, you should have already deployed a website on Google Cloud Platform.

There are 4 steps in this tutorial:

  • 1. Connect to VM Instance
  • 2. Open Apache Config File
  • 3. Edit Apache Config File
  • 4. Restart Apache

1. Connect to VM Instance

google cloud mennu
Go to your Google Cloud homepage and click the hamburger menu in the upper left-hand corner.
ssl certificate for wordpress on google cloud go to compute engine then vm instances
Go to your Compute Engine, then to VM instances to access your VM instance.
ssl certificate setup for wordpress on google cloud
Click on the SSH button to connect to your web server.

2. Open Apache Config File

In this example I am going to redirect eborchids.com/post1 to eborchids.com/post2.

google cloud apache2 configuration file
The first step is to open your Apache configuration file. The command used to open your Apache config file will vary depending on your software configuration.

For most web projects, run the following command:

sudo nano /etc/apache2/apache2.conf

For Bitnami web projects, run the following command:

sudo nano /opt/bitnami/apps/wordpress/conf/httpd-app.conf

3. Edit Apache Config File

google cloud apache 301 redirect
After you’ve opened your Apache configuration file, scroll to the bottom of the page and paste the 301 redirect command.

To redirect eborchids.com/post1 to eborchids.com/post2

Redirect 301 /post1 https://eborchids.com/post2

4. Restart Apache

For changes to take effect, restart your Apache server.

For most web projects, run the following command:

sudo service apache2 graceful

For Bitnami web projects, run the following command:

sudo /opt/bitnami/ctlscript.sh restart apache

Did it Work?

Make sure to test your 301 redirect to ensure that they’re configured properly.

If you have any questions or comments, please post them below!

Up Next…

google cloud ftp setup filezilla
Manage WordPress Files on Google Cloud Platform (Beginner)
manage wordpress files on google cloud
Manage WordPress Files on Google Cloud Platform (Advanced)

Related Posts

  • How to Access phpMyAdmin via SSH Tunnel (GCP)
  • Domain Name Configuration for WordPress on Google Cloud
  • Setup Automatic Backups for Websites on Google Cloud
  • Click-to-Deploy vs. Bitnami for WordPress on Google Cloud
  • Setup Free SSL for WordPress on Google Cloud (Click-to-Deploy) (Latest)
  • The Most Important Linux Commands (With Examples)
FacebookTweetLinkedInEmail

Filed Under: GCP Networking, Google Cloud Platform Tagged With: 301 redirects, apache, gcp, google cloud platform, linux, ssh, terminal

Comments

  1. Russ says

    June 29, 2020 at 9:55 am

    Hello Joy,

    How to make 301 redirect from www to non www of the website, for example from https://www to the main URL https://

    Couple of SEO websites show an error:

    Warning, no 301 redirects are in place to redirect traffic to your preferred domain. Pages that load successfully both with and without www. are treated as duplicate content!

    Not all versions of your page point to the same URL.

    Best Regards,
    Russ

    Reply
    • Leron Amin says

      July 2, 2020 at 9:55 pm

      Hi Russ,

      Which SSL tutorial did you follow?

      Please let me know and I will provide the appropriate instructions for configuring the www to non-www redirect.

      Talk to you soon,
      Joe

    • Russ says

      July 16, 2020 at 2:52 pm

      Hi Joy

      Thanks for your reply and I apologize for late response.
      I was using latest on your website – Setup Free SSL for WordPress on Google Cloud (Bitnami) (Latest)
      https://onepagezen.com/free-ssl-certificates-wordpress-google-cloud-bitnami/

      Please send me the instructions how to redirect from www to the main non www domain

      Thank you
      Russ

    • Leron Amin says

      July 20, 2020 at 8:00 pm

      Hi Russ,

      In step 4, you would need to configure the RewriteRule to be the non-www version of your domain.

      Additionally, you need to follow step 7 of this tutorial and change your website’s URL to the preferred version in your wp-config.php file.

      Let me know if you have questions,
      Joe

    • Russ says

      July 24, 2020 at 12:05 am

      Hello Joe

      Thank you very much, you are awesome! I figured it out.

      I have one more question

      Can you please send me commands if possible, how to make writable htaccess file for bitnami. For example, if I want to add some code to the file from Really Simple SSL and after make it again not writable.

      Best Regards
      Russ

    • Leron Amin says

      August 4, 2020 at 10:26 pm

      Hi Russ,

      I’d recommend using the bncert-tool to configure your SSL certificates, instead of Really Simply SSL.

      You can view the configuration instructions for the bncert-tool here. Alternatively, you can simply run the following command to go through the bncert-tool configuration process:

      sudo /opt/bitnami/bncert-tool

      Let me know if you have questions,
      Joe

  2. Jitu says

    August 9, 2019 at 11:26 am

    Hello Joe
    I want to know how to redirect eborchids.com to onepagezen.com

    Reply
    • Leron Amin says

      August 12, 2019 at 6:10 pm

      Hi Jitu,

      Most DNS registrar’s (the company through which you purchased the rights to your domain name) will offer domain forwarding services, which are typically integrated into their platforms.

      Let me know if you run into any issues setting up the service,
      Joe

  3. jitu tripathi says

    July 31, 2019 at 12:23 am

    i have two same domain with different extention. how to use together in one website

    Reply
  4. Luke says

    June 6, 2019 at 6:46 pm

    Hi,
    My website seems to be stuck in a 301 redirect loop that I can’t seem to fix. Any ideas on how I can fix this? (currently can’t access WordPress)

    Reply
    • Leron Amin says

      June 8, 2019 at 3:23 pm

      Hi Luke,

      The re-direct loop could be caused by functionality in your Apache configuration file.

      When did the error start occurring? If you can attribute it to a certain redirect rule, you should remove it and then proceed to restart your Apache server.

      Let me know if you have questions,
      Joe

    • Luke says

      June 12, 2019 at 2:18 am

      Thanks Joe,

      I didn’t make any changes to my apache or implement any redirect rules, so I’m struggling to attribute anything to this redirect rule.

      The only change I made the day it went down was update some metadata through Yost.

      In the meantime, I reset my apache with no luck. I also tried removing my CNAME in the hope that I would still be able to gain access to my site again, this led to a DNS error.

      Any other ideas will be much appreciated.

      Regards,
      Luke

    • Leron Amin says

      June 17, 2019 at 5:52 pm

      Hi Luke,

      Try removing the redirect rule from your Apache configuration file (wordpress.conf for Click-to-deploy, and bitnami.conf for Bitnami).

      Having multiple redirect rules in multiple configuration files is typically what causes the issue.

      Hope this helps and let me know if you have any questions,
      Joe

  5. patel says

    April 24, 2019 at 1:28 am

    how redirect example.com, http://example.com, http://www.example.com to https://www.example.com in google cloud platform with apache2 .

    Reply
    • Leron Amin says

      May 1, 2019 at 6:21 pm

      Hi Patel,

      You could configure an HTTP to HTTPS redirect (or rewrite) in your Apache configuration file – similar to what’s shown in step 4 of this tutorial.

      Let me know if you have questions,
      Joe

    • patel says

      May 11, 2019 at 11:55 am

      where i put my redirect code in apache2.conf file(give a code)

    • patel says

      May 11, 2019 at 11:56 am

      i don’t use bitnami

    • Leron Amin says

      May 20, 2019 at 6:25 pm

      If you’re not using Bitnami, check out step 5 of the Click-to-deploy SSL tutorial.

  6. Vish says

    April 18, 2019 at 2:25 am

    Great. After hoping to 100’s article, finally it helped. Mine was Bitnami and you saved my life. ! Kudos!

    Reply
    • Leron Amin says

      April 19, 2019 at 9:47 pm

      Happy to hear it! Thanks for the feedback Vish!

  7. Claire says

    December 30, 2018 at 11:27 pm

    Can you please help me with applying X-Frame options to my site on GCP?
    I have tried to paste the code I see on this, in the .htaccess file and in this apache file but nothing works.
    I really enjoy your tutorial and I hope you can help me.
    Thank you

    Reply
  8. Kevin Were says

    October 21, 2018 at 4:36 pm

    This tutorial was great!

    Very straight foward and helped me solve my problem. Mine issue was that the friendly urls were not working and I needed to update the config file

    Reply
    • Leron Amin says

      October 21, 2018 at 11:01 pm

      Thanks for your feedback Kevin – I’m glad to hear you were able to get it working properly!

  9. Benoy says

    September 9, 2018 at 11:28 am

    You saved my life. Your tutorials have helped me through and through. Thought I would leave you a thank you note. I am redoing my whole thing and your tutorials are helping me a lot. You make it so easy. Thanks a lot for your efforts!

    Reply
    • Leron Amin says

      September 10, 2018 at 8:58 am

      Thanks for the feedback Benoy – I’m really glad to hear that the tutorials have been helpful to you!

  10. Stefan says

    June 23, 2018 at 4:17 am

    Hey Leron, I tried really everything in your tutorials and still I have the same problem. My website is zivizdravo.in.rs and when I try to go in admin wordpress console I get redirection problem. I changed everything like you said but nothing helps. Is there any other solution, please i am desperate. Thank you.

    Reply
    • Leron Amin says

      June 23, 2018 at 2:55 pm

      Hi Stefan,

      This tutorial is for configuring redirects for existing pages, and may not be appropriate given your circumstances.

      What is the issue? There is a redirect on your homepage? Please provide details regarding exactly what is happening, including the error messages (if any) that you’re seeing when you’re redirected.

      Talk to you soon,
      Joe

  11. Pankaj Sinha says

    June 11, 2018 at 8:06 am

    your tutorials are very helpful. But I am using digital ocean one click deploy wordpress. Can u post an article on how to redirect http to https on digital ocean? That would be so much appreciated

    Reply
  12. Graham says

    April 25, 2018 at 3:37 am

    Hello, Leron

    Thanks very much for all of your tutorials, it really helped me a lot to setup the website.

    However, I’m just wondering and confused on how to point two domains to a same website in Google cloud?
    For example, I have a http://www.AAA.com and another alternative domain http://www.BBB.com, I would like to point them all to the same URL http://www.AAA.com.

    Is it as same as this tutorial that 301 redirect the url? Or is there a better way to do it?
    I do have searched on Google but found most of the tutorial was to setup in a server which already has a built-in function to do it.

    Much appreciated if you could give me some tips.
    Have a good time !
    Best Regards
    Graham

    Reply
    • Leron Amin says

      April 25, 2018 at 4:19 pm

      Hey Graham,

      This tutorial doesn’t apply to the situation you’re describing.

      Your domain name provider for http://www.BBB.com should offer domain redirection as a built in service, also known as domain forwarding.

      Simply do a Google search for ‘your domain name provider’ + ‘domain forwarding’.
      For example, if your domain name provider is GoDaddy you would search Google for ‘GoDaddy domain forwarding’.

      Good luck and let me know if you have any questions,
      Joe

    • Graham says

      April 27, 2018 at 12:32 am

      Hi, Joe
      Thank you very much for the instant reply for this newbie question.
      I didn’t really know “forwarding” means the same thing of the “Redirection.”
      Problem resolved! šŸ˜€
      Cheers
      Graham

    • Leron Amin says

      April 27, 2018 at 9:46 am

      Hey Graham! I’m glad to hear you were able to get the problem resolved! šŸ™‚

  13. Marian says

    March 2, 2018 at 2:12 am

    hello Joe
    I have a problem
    everywhere it shows me that I have 302 redirects on the entire site
    how can i change it in 301?
    I have SSL certificate
    are hosted in GCC and use Bitnami
    but I understand that the 302 redirection for SEO does not look good
    almost forgot
    SSL certificate, I’ve also implemented it with you (through your video)

    Reply
    • Leron Amin says

      March 3, 2018 at 11:39 pm

      Hi Marian,

      Check out this tutorial on configuring redirects for your website.

      301 and 302 redirects have different use cases, one can be better/worse for your SEO depending on what you’re trying to accomplish.

      Please let me know if you have any questions,
      Joe

    • Marian says

      March 5, 2018 at 1:27 am

      Hello!
      I find 302 on the main page.
      not on the article or other page.
      HTTP / 1.1 302-Found http://www.vegetarianchicken.com/
      HTTP / 1.1 200 OK – https://www.vegetarianchicken.com/
      that’s what I’m trying to fix
      can you help me?
      thank you!

    • Leron Amin says

      March 8, 2018 at 11:07 am

      Hi Marian,

      The redirect is likely what’s configured in your Apache .conf file, telling your server to redirect any inbound HTTP traffic to the HTTPS version of your website.

      Try using the following code to replace the existing 3 lines in your bitnami.conf file, as shown in step 6 of this tutorial, however, with the “permanent” condition added.

      ServerName vegetarianchicken.com
      ServerAlias www.vegetarianchicken.com
      Redirect permanent / https://www.vegetarianchicken.com/

      Also, here is a link from a Google representative saying that over time, they will treat a 302 as a 301 if it appears as permanent – so having a 302 instead of a 301 long-term shouldn’t adversely affect your SEO.

      Talk to you soon,
      Joe

    • Marian says

      March 5, 2018 at 9:30 am

      Hello Joe,
      again šŸ™‚
      can you guide me to my problem?
      get on the main page, redirect 302
      that is, from http, it redirects me to https with 302
      and I want to make it permanent (301)
      When I set up the site, I configured it according to the instructions in your tutorials.
      Now I do not know where I’m wrong,
      but I think google avoids me due to this redirect (302)
      Thanks in advance

  14. Benjamin Waller says

    February 19, 2018 at 8:23 pm

    Hello Joe,
    thanks for the tutorial. Once again you provide excellent content on this topic.

    I have a couple of question:
    Can I use this command to redirect the home page of my site to a login page instead? (Moodle Bitnami stack)

    So the home page is https://www.hocvietngu.com/ but I would like to redirect
    all traffic to https://www.hocvietngu.com/login/index.php

    I want to do this because my goal is to have a WordPress site for the marketing of the site while the Moodle site is the LMS. So that down the track, I link uses to the login page not a landing page.

    Best regards,
    Ben

    Reply
    • Leron Amin says

      February 19, 2018 at 9:32 pm

      Hi Ben,

      I’ve never tried doing a 301 redirect of a homepage, so I am curious if it would work.

      However, I think it won’t work and will probably break your site because there is already an http to https redirect configured for your homepage (index).

      I think the way to do this would be to change your homepage URL in your config.php file to https://www.hongvietngu.com/login/index.php, so as to avoid the redirect. That’s how I would do it in Bitnami’s WordPress stack at least (in wp-config.php). However, I have not tested this so I am not sure how well it would work.

      Let me know if you figure out a good solution.

      Talk to you soon, šŸ˜€
      Joe

    • Benjamin says

      February 20, 2018 at 12:39 am

      Hi Joe,
      Thank for the reply.

      So are you suggesting I change the login url at the direct line in the bitnami config file (/opt/bitnami/apache2/conf/bitnami/bitnami.conf)

      to include the login url like this:
      ServerName http://www.hocvietngu.com
      ServerAlias hocvietngu.com
      Redirect / https://www.hocvietngu.com/login/

      or in the moodle config file ( /opt/bitnami/apps/moodle/htdocs/config.php)

      in the following lines of code??
      if (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] == ‘on’) {
      $CFG->wwwroot = ‘https://www.hocvietngu.com’;
      } else {
      $CFG->wwwroot = ‘http://www.hocvietngu.com’;
      };

      Cheers,
      Ben

    • Leron Amin says

      February 20, 2018 at 1:14 am

      Hi Ben,

      In your Bitnami config, you would use:

      ServerName  hocvietngu.com
      ServerAlias www.hocvietngu.com
      Redirect permanent / https://www.hongvietngu.com/login/

      Hope this helps, and let me know if it works.

      Thanks,
      Joe

    • Benjamin says

      February 22, 2018 at 12:28 am

      Hi Joe,

      I solved this one by turning on a Moodle setting that allows the admin to ‘Force users to log in’ which changes the PHP on the backend. So all good. If you go to https://www.hocvietngu.com/ you will automatically be taken to the login page,
      Thanks for the help again Joe. Much appreciated. šŸ™‚

      Cheers,
      Ben

    • Leron Amin says

      February 22, 2018 at 11:59 am

      Hey Ben,

      I’m glad you were able to get it worked out.

      Your method of forcing users to login sounds like the best way to do it (definitely better than configuring a redirect).

      Thanks for the update,
      Joe

  15. Leron Amin says

    August 25, 2017 at 8:24 pm

    What did you think of this tutorial?
    Please share your questions and comments below!
    Thanks,
    Joe

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Want Exclusive Tutorials?

Welcome to One Page Zen!

Through this blog I hope to introduce web developers to cloud computing!

author-bio

facebook-support-group kinsta-cloud-hosting

Want Exclusive Tutorials?


Contact | Sitemap | Legal | Privacy Policy | Affiliate Disclosure
      Copyright Ā© 2017 - 2019 One Page Zen
Share this Article

Email sent!