1

When users come through the root level of the domain, ie: http://foo.com, I want them to be redirected to http://www.foo.com

How should I go about doing this?

Sample .htaccess-file

Thanks to @HendrikEng for his help and clarification. Based on what he said and what I found at https://www.hashtagerrors.com/blog/the-perfect-htaccess-file-for-craft I'm using this .htaccess file. Feel free to copy.

The reason I am redirecting to a specific domain instead of using @HendrikEng's suggestion is that I am running in a multi staging environment (at Hyperlane), and needed to avoid redirects to www. when running on their development or staging environments.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

    # Add https://www to beginning of URL 
    RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]    

    # Mime types
    # Audio
    AddType audio / ogg .oga ogg
    AddType audio / mp4 .m4a.f4a.f4b
    # Video
    AddType video / ogg .ogv
    AddType video / mp4 .mp4.m4v.f4v.f4p
    AddType video / webm .webm
    AddType video / x - flv .flv
    # Images
    AddType image / x - icon .ico
    AddType image / webp .webp
    AddType image / gif .gif.GIF
    AddType image / jpeg .jpeg.jpg.jpe.JPG
    AddType image/svg+xml .svg .svgz
    # Fonts
    AddType application / vnd.ms - fontobject .eot
    AddType application / x - font - ttf .ttf.ttc
    AddType font / opentype .otf
    AddType application / x - font - woff .woff
    # JAVASCRIPT
    AddType application / javascript .js.jsonp
    AddType application / json json
    # Extras
    AddType text / x - component .htc
    AddType application / xml .rss.atom.xml.rdf
    AddType application / x - chrome - extension .crx
    AddType application / x - opera - extension .oex
    AddType application / x - xpinstall .xpi
    AddType application / octet - stream .safariextz
    AddType application / x - web - app - manifest + jsonp .webapp
    AddType text / x - vcard .vcf
    AddType application / x - shockwave - flash .swf

</IfModule>

<IfModule mod_expires.c>
    ExpiresActive on
    # Perhaps better to whitelist expires rules? Perhaps.
    ExpiresDefault "access plus 1 month"
    # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
    ExpiresByType text/cache-manifest       "access plus 0 seconds"
    # Your document html
    ExpiresByType text/html                 "access plus 30 minutes"
    # Data
    ExpiresByType text/xml                  "access plus 0 seconds"
    ExpiresByType application/xml           "access plus 0 seconds"
    ExpiresByType application/json          "access plus 0 seconds"
    # Feed
    ExpiresByType application/rss+xml       "access plus 1 hour"
    ExpiresByType application/atom+xml      "access plus 1 hour"
    # Favicon (cannot be renamed)
    ExpiresByType image/x-icon              "access plus 1 week"
    # Media: images, video, audio
    ExpiresByType image/gif                 "access plus 1 month"
    ExpiresByType image/png                 "access plus 1 month"
    ExpiresByType image/jpeg                "access plus 1 month"
    ExpiresByType image/svg+xml             "access plus 1 month"
    ExpiresByType video/ogg                 "access plus 1 month"
    ExpiresByType audio/ogg                 "access plus 1 month"
    ExpiresByType video/mp4                 "access plus 1 month"
    ExpiresByType video/webm                "access plus 1 month"
    # HTC files  (css3pie)
    ExpiresByType text/x-component          "access plus 1 month"
    # Webfonts
    ExpiresByType application/x-font-ttf    "access plus 1 month"
    ExpiresByType font/opentype             "access plus 1 month"
    ExpiresByType application/x-font-woff   "access plus 1 month"
    ExpiresByType image/svg+xml             "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
    # CSS and JavaScript
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
</IfModule>

<IfModule mod_rewrite.c>
   RewriteCond %{SCRIPT_FILENAME} -d [OR]
   RewriteCond %{SCRIPT_FILENAME} -f
   RewriteRule "(^|/)\." - [F]
</IfModule>

<FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
   Order allow,deny
   Deny from all
   Satisfy All
</FilesMatch>

<IfModule php5_module>
    php_value session.cookie_httponly true
</IfModule>

1 Answer 1

2

It's not really Craft related, but assuming that you use .htaccess you could set up rewrite conditions to redirect the request:

# Rewrite Engine
RewriteEngine On

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

# force www
RewriteCond %{HTTP_HOST} ^foo.com [NC]
RewriteRule ^(.*)$ http://www.foo.com/$1 [L,R=301,NC]
1
  • Hey, yeah! You're right. It's not Craft-related. I ended up using .htaccess too. Not tested it in production yet -but Hyperlane- which is my hoster, seems to be using the .htaccess file. A little learning curve for one who's spent much of his life in the Microsoft world ;-)
    – nitech
    Commented Jun 27, 2019 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.