0

I would like to redirect for 2 situations:

  1. non-www
  2. https

I currently use the following in my .htaccess file:

RewriteEngine on #RewriteCond %{HTTP_HOST} ^www\.egps\.org [NC] #RewriteRule ^(.*)$ https://website.org/$1 [L,R=301] RewriteCond %{SERVER_PORT} ^80$ RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

I commented out the first 2 lines as they caused a problem. While searching Stackoverflow for answers I found the following accepted answer:

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

Can someone please explain how I might alter this code to redirect to NON www instead of www?

Can anyone translate into very simple language what these lines of code are actually saying? This is what I gather so far...

  1. If URL does not have www
  2. If URL is not preceded by https
  3. Not sure about this one
  4. Rewrite URL by adding HTTPS://www. URL

1 Answer 1

1

I recommend to implement two separate redirection rules. This has the advantage that it only leads to a single redirection for all situations and is far easier to read / maintain:

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

That first condition extracts the part of the host name without the leading www..

Those rules work in the http servers host configuration and likewise in dynamic configuration files (.htaccess style files).


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Sign up to request clarification or add additional context in comments.

4 Comments

Where would I accomplish these redirects? I use cPanel and do see a link for redirects but it is not clear how to enter your suggestions. Also within my .htaccess file I have the following:
Sorry...couldn't edit my comment above. Where would I accomplish these redirects? I use cPanel and do see a link for redirects but it is not clear how to enter your suggestions. I use .htaccess for other situations like displaying a maintenance page or 404 page.
I am experiencing a problem with the code above. If I type example.org (without www or https://), I get example.org%7Brequest_uri%7D’s server DNS address could not be found. Also, if I type example.org, I get the same error message. Do we need additional redirects to handle if someone enters http:// or doesn't enter any 'preface' at all?
The fact if you enter a protocol scheme before the host name does not matter when using a browser, the browser always uses a protocol scheme, it is just not visualized. Questionable strategy, but such are today's implementations. The "percent escaping" you point out surprises me... that is exactly what the NE flag is for: to prevent escaping of "special characters". Are you really certainly that you use the NE flag as I showed in the code above? I assume you dropped it when copying the rules...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.