Some generic htaccess redirects

RapidWeaver 8 added a nice little htaccess file editor; it can be found under the publishing settings:
2020-03-16_13-15-08

Here are a couple of simple htaccess file redirects for common conditions.

These are generic, not URL specific, so these should NOT require any modifications.

Force HTTPS and Remove WWW:

#  Force HTTPS and Remove WWW (this is a comment)
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Force HTTPS and force WWW:

#  Force HTTPS and force WWW (this is a comment)
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Some things to keep in mind

  • .htaccess file only works on Apache webserver (and only if allowed)
  • htaccess directives MUST be Plain text so if you are copy and pasting from another app or the web take care to make sure they are plain text
  • Make sure you have fixed all mixed content errors before implementing the redirects (active mixed content will Break an existing working site)
  • you can test the htaccess file redirects before implementing them with the handy tester by made with love.
  • when redirecting with 301’s, they are permanent and cacheable, so make sure they work before making them 301’s. Change the r=301 to r=302 until you have tested them thoroughly.
10 Likes

Thanks Doug this is really useful.

I have a question google is indexing some of my website pages with the extension index.html on the end of some urls (about 50 out of 300). Is there some code I can put in to the htaccess file that will stop this?

I have a sitemap with all the correct urls but google has indexed these pages with index.html on the end and lists them as ‘Indexed, not submitted in sitemap’ in Google Search Console.

You should add redirect rule to forward any .html to .php pages

Removing the index.html from the URL should be able to be done with these two lines:

# remove index.html from base URL(homepage) (this is a comment)
RewriteRule ^index\.html$ / [R=301,L]
# remove index.html from sub-pages(this is a comment)
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

If you add these or any rules to the htaccess file only one RewriteEngine On is needed.

Also, if the rule has both l RewriteCond's and RewriteRule keep them together. They are a set.

So if you want to add these two RewriteRule statements(along with comments), don’t put them in the middle of the htaccess files other rules.

2 Likes
#redirect index.html to index.php (this is a comment)
RewriteRule index\.html$ index.php [NC,R=301,L]

This should work, hover this brings up an issue, and that’s the order of items in .htaccess is important. If you did both the above remove index.html from the URL and then tried to redirect it to index.php, it wouldn’t work. The index.html would get removed so the redirect to PHP wouldn’t happen.

The Modifiers within the [ ] can affect things. The r=301 sets the return code to a 301. The default is 302 temporary. Other common modifiers:

  • NC - nocase match is not case sensitive
  • F - Forbidden returns a 403 Forbidden (same as an r=403)
  • G - gone returns a 410 Gone (same as an r=410)
  • L - Last htaccess will stop feather processing if this is met.

Another directive that is useful would be this:

DirectoryIndex index.php index.html index.cgi

Tells Apache what to look for if the full filename isn’t specified, and the order to look(left to right).
So the above will look for an index.php before the index.html.

2 Likes

Thanks for your help @teefers. Previously my htaccess file just forced https. Used this code and it now forces https, non-www & solves the problem I was having with index.html url extensions.

2 Likes