8 – Redirect domainname from non-www to www in settings.php in combination with https

Because the .htaccess gets overwritten when you update your Drupal Core, I found an other way to implement redirection to https. In settings.php. But below function does not redirect non-www to www.
I would like to extend this function so all domains without www are redirected to www.domain.com.

Below function was found on: https://www.drupal.org/https-information

if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
  if (substr($_SERVER('HTTP_HOST'), 0, 4) <> 'www.') {
    $new_url = 'www.' . $_SERVER('HTTP_HOST');
  } else {
    $new_url = $_SERVER('HTTP_HOST');
  }
  $new_url .= $_SERVER('REQUEST_URI');

  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://'. $new_url);
  exit();
}

With above function:

http://www.mydomain.com redirect to https://www.mydomain.com ==> OK

http://mydomain.com redirect to https://mydomain.com ==> Not OK

I want:

http://mydomain.com or https://mydomain.com to https://www.mydomain.com

Hope this helps.

http request – Redirect non-www to www in settings.php for Drupal 8

I want to redirect all non-www to www in below function.

if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
  if (substr($_SERVER('HTTP_HOST'), 0, 4) <> 'www.') {
    $new_url = 'www.' . $_SERVER('HTTP_HOST');
  } else {
    $new_url = $_SERVER('HTTP_HOST');
  }
  $new_url .= $_SERVER('REQUEST_URI');

  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://'. $new_url);
  exit();
}

apache – Can I use the .htaccess file or another solution to address www and non-www duplicate content issues?

What is “duplicate content”?

“Duplicate content” is simply where you have two or more different URLs referencing essentially the same resource and are not canonicalising this in some way (eg. with a rel="canonical" element or external redirect). https://example.com/foo/bar and https://www.example.com/foo/bar are two different URLs and are consequently treated differently when setting cookies, client-side caches and search engines. Whilst these two URLs might return the same content on your site (and indeed most sites), technically they might not, so do need to be treated differently.

In terms of SEO, search engines might end up (unnecessarily) dividing crawl budget between the two URL versions. They might choose to favour the non-canonical (ie. non-preferred) URL over the other when returning results in the SERPs. You may get backlinks to both URLs which will potentially divide so called “link juice” between the URLs which could result in neither page ranking as well as if you only have one canonical URL for one resource.

Having said that, search engines can often resolve www vs non-www duplicate content issues themselves, providing there is a strong enough signal. But they can still get it wrong.


Resolving “duplicate content”

You can potentially get a duplicate content issue if you are inconsistently linking (or referencing) both example.com and www.example.com throughout your site.

  • If you are using absolute URLs in your internal links then you need to consistently link to www or non-www. OR, use root-relative URLs starting with a slash and omit the scheme and hostname entirely.
  • rel="canonical" link element in the head section of your HTML needs to reference the canonical hostname.
  • XML sitemap needs to reference the canonical hostname.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 (R,L)

From this HTTP to HTTPS redirect, I assume your canonical hostname uses the www subdomain? If www is not your canonical hostname then that’s obviously an error and should be fixed.

(If you have tested that this is working as expected then this should be a 301 – permanent – redirect, as opposed to a 302 – temporary – redirect, which is what this is currently defaulting to. ie. Change R to R=301 in the RewriteRule flags argument.)

Once you have confirmed/fixed the steps mentioned above then you can implement a non-www to www 301 external “redirect” in your .htaccess file to resolve any URLs that might have been indexed on the non-canonical hostname.

UPDATE: The redirect ensures that only the canonical URL (the www variant in this case) is viewed by users and crawled by search engine bots.

For example, after the HTTP to HTTPS redirect above:

# Redirect example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com (NC)
RewriteRule (.*) https://www.example.com/$1 (R=301,L)

TIP: Always test first with 302 (temporary) redirects to avoid potential caching issues, since 301s are cached persistently by the browser.

.htaccess directives fail to redirect www to non-www URLs

I’m trying to redirect https://www.example.com to https://example.com.

I’m running Ubuntu server 2020 lts with Apache 2, PHP 7 and Let’s Encrypt SSL.

My file structure:

logs/
private/
public/
.htaccess
index.php

My .htaccess file:

RewriteEngine On

# First rule
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ (NC)
RewriteRule ^(.*)$ https://%1/$1 (R=301,L)

# Second rule
RewriteCond %{THE_REQUEST} ^GET /public/ (NC)
RewriteRule ^public/(.*)$ $1 (L,R=301,NE)
RewriteRule ^((?!public/).*)$ public/$1 (L,NC)

cURL output:

curl -I http://www.example.com
HTTP/1.1 301 Moved Permanently
Date: Thu, 01 Apr 2021 13:42:14 GMT
Server: Apache
Location: https://www.example.com/
Content-Type: text/html; charset=iso-8859-1

I don’t understand why the first rule doesn’t redirect as expected.

The second rule works, it hides public. I’m in the public directory but public is invisible in https://example.com/.

.htaccess — www to non-www doesn’t want to work

I’m trying to redirect https://www.test-domain.com to non www https://test-domain.com.

My Sever -> Ubuntu server 2020 lts, apachae2, php7, ssl letsencrypt

File structure:

enter image description here

My .htaccess

RewriteEngine On

/*first rule*/
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ (NC)
RewriteRule ^(.*)$ https://%1/$1 (R=301,L)

/*second rule*/
RewriteCond %{THE_REQUEST} ^GET /public/ (NC)
RewriteRule ^public/(.*)$ $1 (L,R=301,NE)
RewriteRule ^((?!public/).*)$ public/$1 (L,NC)

Output:

curl -I http://www.test-domain.com
HTTP/1.1 301 Moved Permanently
Date: Thu, 01 Apr 2021 13:42:14 GMT
Server: Apache
Location: https://www.test-domain.com/
Content-Type: text/html; charset=iso-8859-1

The second rule hides public and it works. I’m in the public directory but public is invisible https://test-domain.com

htaccess – Cookies for www and non-www different

This is arguably a problem with your server-side application and how you are setting cookies (if this behaviour is undesirable), rather than the browser per se. In order to set a cookie, you determine on what domain (or rather, what part of the current hostname) the cookie is set. If your application is setting two different cookies, one for the www subdomain and one for the domain apex, then it’s setting the cookie on the requested hostname only, rather than the domain apex (and all subdomains).

However, redirecting from one to the other would workaround the issue since it obviously prevents the site being accessible from the non-canonical hostname and the application can’t then set a cookie on it. (Note that browsers will continue to send the Cookie header on the non-canonical hostname, whilst it’s still valid in the browser.)

If the non-www hostname is canonical then you should indeed be redirecting from www to non-www…

RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule (.*) http://example.org/$1 (L,R=301)

This is close, but you have a typo in the RewriteRule pattern that will prevent it from matching “most” URLs… (.*) matches a literal *, which is probably not the intention. It should simply be (.*) to match “everything” (no backslash escape before the *).

You should also presumably be redirecting to HTTPS, not plain old HTTP? And if only have the one domain then you can simplify the condition to match just www at the start of the hostname, rather than matching the entire hostname.

For example:

RewriteCond %{HTTP_HOST} ^www. (NC)
RewriteRule (.*) https://example.org/$1 (L,R=301)

Google hosted domain DNS settings for non-www and www pointing to ghs.googlehosted.com

We are running a google hosted website (google apps).

According to this guide we can set www.domain.com using a CNAME this part this clear

https://support.google.com/sites/answer/9068867?hl=en and https://support.google.com/sites/answer/9068867?hl=en

However, we also want the non-www version to open the google hosted site. And an A record is mandatory I think.

So both www.domain.com and domain.com should point to the google hosted site @ ghs.googlehosted.com

Question: what DNS settings should I use so both www.domain.com and domain.com work with a google hosted website. Another option would be to redirect non-www to www. As long as it works.

Help appreciated

redirects – redirecting from www url to non-www url in Google Firebase static hosting

I am using Google Firebase static hosting.

When a user types and enters www.example.com, I want the address to change to example.com and show the user content of example.com.

How can this be achieved? E.g. In Firebase, can I say that I want that subdomain to be redirected there? I don’t think there is a .htaccess file.

Note:

Answers to this question that advise on dns seems to be only one part of the solution. I talked with my domain name and dns manager (Hostinger) tech support, and they insist that there are things to be done on Firebase.

google pagespeed – Avoid multiple page redirects about from HTTP to HTTPS and from non-www HTTPS to with-www HTTPS

Humbly, In Google PageSpeed Insights test I got 95 score for mobile and 95 score for desktop with pretty much just one error:

Avoid multiple page redirects

The first entry of the error is about HTTP to HTTPS redirects and the second entry of the error is about non-www HTTPS to with-www HTTPS redirects.

The first entry is weird because its Google themselves who passionately promote HTTP to HTTPS usage, so why would they give me an error for doing so?…

seo – Does www and non-www suffer duplicate content penalty?

John’s answer is spot on however if your looking for advice which one is technically the best then the actual the answer is all 3 options.

SOURCE: Pro Webmasters Answer from John Conde

  1. Do a 301 redirect to use the ‘www’ or no ‘www’
  2. Specify your preferred domain in Google Webmaster Tool
  3. Use canonical URLs (although it isn’t typically used in this situation

And here’s why…

Why 301 redirect URLs:

When you force users to use either www or non-www and they decide to reward your site with a backlink the next user who clicks that link does not need to go through a redirect.

While redirects nowadays are near instant they are additional server-side request that can be reduced if you have users linking to you correctly, which should be considered a good practice, especially if your site receives high volume of traffic.

Why canonical URLs

Nowadays it is considered a best practice to use canonical links, this is because duplicate content can appear in many different forms without factoring www and non-www. Blogs particularly will operate with pages being accessible via many different URLS for example:

Using Canonical links correctly will insure that your content however it may be accessed is never marked as duplicate.

Why preferred domain in Google Console

It is a good practice to add all variations to Google Webmaster Tools so that you can confirm at any point that your site or Google is not doing anything it shouldn’t be.

  • NON-SSL sites will have 2 variations
  • SSL sites will have 4 variations

You should seem something similar to this:

bybe multiple sites in webmaster tools
(source: bybe.net)

Adding a preferred domain in the process takes no more than a few seconds but in additional to 301 and canonicals this option is a great fallback just in case for some reason your 301’s or canonical links stop working for one reason or more.