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
In this example I am going to redirect eborchids.com/post1 to eborchids.com/post2.
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
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!
Russ says
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
Leron Amin says
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
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
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
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
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:
Let me know if you have questions,
Joe
Jitu says
Hello Joe
I want to know how to redirect eborchids.com to onepagezen.com
Leron Amin says
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
jitu tripathi says
i have two same domain with different extention. how to use together in one website
Luke says
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)
Leron Amin says
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
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
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
patel says
how redirect example.com, http://example.com, http://www.example.com to https://www.example.com in google cloud platform with apache2 .
Leron Amin says
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
where i put my redirect code in apache2.conf file(give a code)
patel says
i don’t use bitnami
Leron Amin says
If you’re not using Bitnami, check out step 5 of the Click-to-deploy SSL tutorial.
Vish says
Great. After hoping to 100’s article, finally it helped. Mine was Bitnami and you saved my life. ! Kudos!
Leron Amin says
Happy to hear it! Thanks for the feedback Vish!
Claire says
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
Kevin Were says
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
Leron Amin says
Thanks for your feedback Kevin – I’m glad to hear you were able to get it working properly!
Benoy says
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!
Leron Amin says
Thanks for the feedback Benoy – I’m really glad to hear that the tutorials have been helpful to you!
Stefan says
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.
Leron Amin says
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
Pankaj Sinha says
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
Graham says
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
Leron Amin says
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
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
Hey Graham! I’m glad to hear you were able to get the problem resolved! š
Marian says
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)
Leron Amin says
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
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
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.
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
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
Benjamin Waller says
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
Leron Amin says
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
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
Hi Ben,
In your Bitnami config, you would use:
Hope this helps, and let me know if it works.
Thanks,
Joe
Benjamin says
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
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
Leron Amin says
What did you think of this tutorial?
Please share your questions and comments below!
Thanks,
Joe