htaccess Tips

  • With Jamroom 5 the htaccess file does a few things, the most important of which is to push everything through router.php.

    So, gone are the lovely rewrite rules of JR4, and we are all glad to see them go - one less thing to worry about. But there are still a few non-Jamroom things which you might need to do in your htaccess file.

    First a brief introduction to htaccess for those who are not yet experts.

    htaccess files can change the configuration of the webserver on a per directory basis. The rules in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all it’s subdirectories. But a subdirectory can contain a htaccess of it’s own, which can change the Apache config again.

    Things you can do in a htaccess file include password protecting a directory, blocking IP addresses, redirecting requests to the server, and sometimes changing your php upload limits (detailed in this FAQ).

    Powerful stuff, but get one thing wrong in your htaccess file and you are stuck with an internal server error. That’s why .htaccess files begin with a dot, to make them hidden files, because they can really mess things up.
  • Because they are hidden by default, if you are looking at your site in an ftp app you will not see the file listed unless you have set that somewhere in your app’s preferences. Similarly, if you have downloaded the files you may need to change the preferences of your operating system to be able to see them.
  • So what might you need to do in the htaccess file?

    How about forcing the www. in your url?

    RewriteEngine On
    
    # Ensure www on all URLs.
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


    Or get rid of the www altogether?

    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]


    Or to force the use of https://

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://update.ultrajam.net/$1 [R,L]

    Or maybe forcing a trailing slash on urls

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ https://update.ultrajam.net/$1/ [L,R=301]


    Changing Domains?

    Htaccess can be useful when you move your site to a new domain. Put this in the old site’s htaccess and all traffic to your old site will arrive at your new site index page:

    Redirect 301 / http://www.newsite.com/

    But what if you are moving an existing Jamroom to a new domain and need to keep the same page and url structures?

    RewriteCond %{http_host} !^www.oldsite.com [NC]
    RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]

Tags