Forum Moderators: phranque

Message Too Old, No Replies

301 double redirect

         

chainazo

3:24 am on Sep 4, 2024 (gmt 0)

Top Contributors Of The Month



I needed to do 301 to achieve no-www and https.
The HTTPS does it well, but to remove WWW, it makes it first redirecting HTTP and then HTTPs with which I am added an intermediate redirection.
Can't www be removed and then directly redirect to HTTPS?

These are the codes I wrote in htaccess:
Rewriteengine on

Rewritecond %{http_host} ^www \. (.+) $ [Nc]
Rewriterule ^ http%{ship: protossl}: //%1%{request_uri} [l, r = 301]

Rewritecond %{https}! On
Rewriterule (.*) Https: //%{http_host}%{request_uri} [r = 301, l]

lucy24

5:48 am on Sep 4, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Canonicalization can and should be done in a single step. But it isn't clear from your post whether this is a single htaccess covering redirects for multiple sites, hence the %{HTTP_HOST} and so on. Ordinarily each site would have its own RewriteRule.

Rewriterule (.*) Https: //%{http_host}%{request_uri} [r = 301, l]
This line, in particular, makes no sense. Why are you capturing the request if you’re not going to use it in the target?

chainazo

10:02 pm on Sep 4, 2024 (gmt 0)

Top Contributors Of The Month



Thanks for your reply, I almost got it.

This is how it is today: [ibb.co...]

I just need the http://www to go directly to https:// without going through https:www

I Googled for that particular case but I didn't find that one specifically.

lucy24

2:01 am on Sep 5, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You haven't answered the key question: is this single rule meant to cover more than one site? If no, the Condition should give the actual site name, preferably expressed as a negative: “If HTTP_HOST is anything other than this exact form”.

In any case, what you need is one RewriteRule with two attached RewriteCond: one for wrong www, the other for http, with [OR] instead of the default [AND].

Rewritecond %{https}! On

I hope this is an artifact of posting. (The same goes for the wonky casing throughout. Apache directives aren’t necessarily case sensitive, but it’s wise to stick with the standard forms.) It of course needs to be !on

not2easy

10:53 am on Sep 5, 2024 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I have another key question: is this a WP site? It makes a difference.

chainazo

2:33 am on Sep 9, 2024 (gmt 0)

Top Contributors Of The Month



Solved with OR.

Isn´t a WP site.

Thanks for both.

I put code perhaps it is useful for someone else:

Without WWW:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]


With WWW:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]