How to redirect 'http://sub.example.com' to 'https://example.com' in just 1 step

Hi,

I removed a subdomain of my website (sub.example.com), and redirected all the traffic to ‘example.com’, through the Apache htaccess configuration:

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

However the non-HTTPS traffic (e.g. ‘http://sub.example.com/product’) needs two 301 redirects:

  1. http://sub.example.com/product --301–> https://sub.example.com/product (through the Cloudflare configuration of ‘Always Use HTTPS’)
  2. https://sub.example.com/product --301–> https://example.com/product (through my Apache httaccess)

These two steps slow down the speed, and make Googlebot to crawl two times my URLs.

Is there any way in Cloudflare to save one step from ‘http://sub.example.com’ to ‘https://example.com’?

Thank you.

Create a redirect rule here…
https://dash.cloudflare.com/?to=/:account/:zone/rules/redirect-rules

Like this…

2 Likes

Thank you very much @sjr.

Why a ‘Redirect rule’ and not a ‘Page rule’? I do not understand the differences.

As far as I understand after reading

I can create the following Page rule:

*sub.example.com/*
Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://example.com/$2 )

Am I wrong?

Page rules will be deprecated at some point. It is best to create all new redirects in redirect rules.

3 Likes

Thank you, @sjr.

Does it mean that I should replace as soon as possible my existing Page Rules by new Redirect Rules?

Hi,

I want to create one Redirect Rule to redirect from
http://example.com/photos/ANYLABEL (non HTTPS version)
to
https://example.com/product/ANYLABEL/#photos (HTTPS version)
where ‘ANYLABEL’ is the label of the product.

As far as I understand, this is a regex issue. I’ve got a Pro plan in Cloudflare, not sure if I’m allowed to this.

Currently, I’m doing this in Apache .htaccess. However, this involves two 301 redirects:

  1. http://example.com/photos/ANYLABELhttps://example.com/photos/ANYLABEL (performed in the ‘Always Use HTTPS’ of Cloudflare)
  2. https://example.com/photos/ANYLABELhttps://example.com/product/ANYLABEL/#photos (performed in origin’s Apache .htaccess)

Any help is appreciated

Have a look at this:

As your label always starts at the same position and is also the end, you should be able to retrieve it with the substring function.

You can then use the concat function to build your new url.

3 Likes

Thank you @Laudian,

However, I’m still getting two 301 redirects with the subdomain:

1) http://sub.example.com/photos/name-33 --> https://sub.example.com/photos/name-33
2) https://sub.example.com/photos/name-33 --> https://example.com/product/name-33#photos

As far as I understand:

  1. The first 301 redirect is due to the fact that I switched on “Always Use HTTPS” in ‘SSL/TLS - Edge Certificates’
  2. The second 301 redirect is a RewriteRule on my origin’s Apache server (htaccess).

Should I switch off the “Always Use HTTPS” in ‘SSL/TLS - Edge Certificates’ and create a new Page Rule (in the second position)?:

- URL: *example.com*
- Setting: Always Use HTTPS

And then create another Page Rule (in the very first position, to catch URLs before the ‘Always Use HTTPS’)

- URL: http://amp.example.com/photo/*
- Setting: Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://example.com/product/$1/#photos)

I’m aware that I’m losing one of my 20 page rules (Pro account), but it may help me.

Do you see this in a browser or with some other application?

Cloudflare uses https records to tell browsers to always use https, so you really shouldn’t need to worry about https redirects.

This redirect would show up in the browser’s dev console as an internal redirect - 307. This doesn’t actually send a request which then needs to wait for a response before being redirected, the browser just changes the scheme to https.

So you should just keep always use https activated and then create the redirect as you want it.

1 Like

Do you see this in a browser or with some other application?

I see it with any online redirection checker, and also with “curl -v” in my Linux shell.

I’ve just checked again that I had HTTP Strict Transport Security (HSTS) enabled in my Dashboard, with a Max Age Header (max-age) of 6 months (the recommended value). However, I do not see any 307-redirect.

I’ve just checked that ‘Always Use HTTPS’ is also enabled.

If it’s useful, my SSL/TLS encryption mode is Full.

Neither curl nor any online redirect-checkers are browsers, so you can not see an internal-redirect. HSTS does not have any effect on these tools either.

Is it important for you that these tools only show a single redirect , or is the hostname used to display a website in browsers?
Because in that case, I’d recommend you check with your browsers development console (F12, Network tab).

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.