Shpigford
Shpigford

Reputation: 25368

www-removal via .htaccess forwards to index.php

What I ultimately am trying to do is forward any URL with www to it's non-www equivalent. So https://www.baremetrics.io/academy would automatically forward to https://baremetrics.io/academy.

Instead, those www pages forward to https://baremetrics.io/index.php

And I'm stumped on why. Hoping there's something in the .htaccess file that I'm overlooking.

Here is the contents of the .htaccess file for baremetrics.io:

Header set Access-Control-Allow-Origin "*"

# Turn on the Rewrite Engine
RewriteEngine On

# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /

# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]

# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]

# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1

# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]

For what it's worth, the site is deployed to Heroku.

Upvotes: 1

Views: 124

Answers (1)

Prix
Prix

Reputation: 19528

The issue is that your other rules are taking place first which causes your non-WWW redirect to not trigger.

You can move your # No WWW rule right after RewriteBase / which should fix it.

Header set Access-Control-Allow-Origin "*"

# Turn on the Rewrite Engine
RewriteEngine On

# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /

# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]

# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]

# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]

# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1

Upvotes: 1

Related Questions