First Things First

Submitted by Marc on Mon, 01/29/2018 - 20:50
Retro Pi - unrelated to Drupal

Starting off with Drupal 8 isn't much different than Drupal 7. Same installation process, same configuration, roughly similar interface once you're in. What's different for me at this stage is a change in hosting. I don't consider myself a "DevOp" in that I don't know much about and don't typically handle server configurations for websites. I'm a developer through and through so I focus on the specific web technologies like PHP, HTML, CSS, JavaScript and the like. So, when it came time to spin up a Drupal 8 site from scratch, I ran into a few bumps.

For my everyday job these days, we use Pantheon for Drupal hosting and they seriously rock. One terminus command or a click of a button on a dashboard and I've got a site fully ready to go. But, with my hosting provider for my personal sites, I wanted to try things all on my own. So, here are a few of the roadblocks I ran into and how to solve them.

htaccess vs PHP redirects

This was a tricky one for me. I've never been good at .htaccess files. It has something to do with my eyes crossing when it gets to the regular expressions. Sure, give me a good cup of coffee and an hour and I'll figure it out, but I've traditionally shied away from writing my own with the exception of some shell scripts I've written. The point is, when I attempted to copypasta from the Internets, I found that my site didn't load at all. Bummer.

My goal was to redirect all non-www and all non-https traffic to the www and https version of the site. On Pantheon, they recommend using PHP and the following code worked great for me:

if (PHP_SAPI !== 'cli') {
$primary_domain = 'www.codechefmarc.com';
if ($_SERVER['HTTP_HOST'] != $primary_domain || !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ) {
  header('HTTP/1.0 301 Moved Permanently');
  header('Location: https://'. $primary_domain . $_SERVER['REQUEST_URI']); exit();
} 
 
// Drupal 8 Trusted Host Settings
if (is_array($settings)){
  $settings['trusted_host_patterns'] = array('^'. preg_quote($primary_domain) .'$');
}

However, when I thought about it for a few, I realized that the .htaccess is ultimately the better way to go and to troubleshoot why it wasn't working. Well, my first part was correct:

#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^codechefmarc.com [NC]
RewriteRule ^(.*)$ https://www.codechefmarc.com/$1 [L,R=301,NC]

#Force https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But I needed this (source):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I think because of the host I'm on specifically? If the file or directory doesn't exist, then send to the index.php file which loads Drupal as normal. I think this is because Drupal is ultimately built on a database with pathauto and such taking over so there are really no "files" or "directories". Since I've been spoiled with Pantheon and the PHP redirects, I've not known how to do the .htaccess until now. Good to know!

PHP_SAPI and cli for Drush

Since I've relied on Pantheon's terminus commands for running Drush commands, I've been a little spoiled by not knowing exactly what this PHP_SAPI constant was all about. Turns out, it's used to figure out if PHP was called via a http request vs a command line interface. Drush doesn't like redirects (drupal_goto();) so we need to exempt the redirects (as stated above) when running Drush from the command line. Neat.

D8 Theming

What the Bartik? For the moment, I'm sticking with the default Drupal 8 theme (Actually, I created a sub-theme of Bartik for the moment so I could add in the automatic highlighted code blocks above), and will switch it up soon as part of my training. I recently dove into CSS Grid headfirst with the amazing training from Wes Bos and my goal is to attempt my own theme without using frameworks like Bootstrap or Foundation. We'll see!