.htaccess url rewrite with http:// and https://

I thought I would share this, if anyone ever came across this.

Scenario

We had a online shop on a http:// domain. and on the same http:// domain we had 3 other sites in sub folder lets call it /site1, /site2 and /site3 so we had:

The online shop – http://www.ourdomain.co.za/
Site 1 – https://www.ourdomain.co.za/site1
Site 2 – https://www.ourdomain.co.za/site2
Site 3 – https://www.ourdomain.co.za/site3

Obviously our shop had categories for example http://www.ourdomain.co.za/category-1

So we decided to move the online shop to a secure site thus changing the http: to https:.
Now in a SEO perspective this is bad because google see’s this as n whole new site and you will loose your ranking, a simple 30 redirect in the index.php on the old root will tell google you have a new site but it still leaves you with a bunch of broken links because http://www.ourdomain.co.za/category-1 will not give you the category anymore it would rather take you to the new home page.. not Ideal.. So how do you redirect ALL old URL’s to the new site and there respective places and still keep Site1, Site2 and Site3 working.. We use .htaccess.

ok so if you open your .htaccess the first line will be:

RewriteEngine On

This tells apache will be rewriting the urls

Now we add a conditional statement tho check if the protocol is using port 80 ( which typically is unsecure site ) then carry on to the second condition

RewriteCond %{SERVER_PORT} ^80$

Next is the line to tell apache which folders we want to exclude from the Rewrite Rule, this will be Site1, Site2 and Site3:

RewriteCond %{REQUEST_URI} !(Site1|Site2|Site3) [NC]

And lastly we apply the actual rewrite rule to change all other urls from http:// to https://

RewriteRule ^(.*) https://www.ourdomain.co.za/$1 [L,R=301]

Save and refresh.. Now there may be other ways of doing this but this is how I did it and it works for me.

If you have questions, comments drop it in the comments. 🙂

Janes Oosthuizen

Author Janes Oosthuizen

Programmer and Tech Junky from Bloemfontein, South-Africa. I have been programming for 8 years in various languages including ( CSS, HTML, jQuery, PHP, MySQL, Wordpress and many other ).

More posts by Janes Oosthuizen

Leave a Reply