Friday, March 29, 2013

Dev on your Mac? How to edit host file for testing!

OK so, you are working on your Mac and want to test a website in your browser... but the domain is not yet delegated - so you only have IP address for your dev server or new web hosting.

What you need to do is edit your host file, so that your browser checks this first before the domain DNS.

  1. Open a terminal window. (Applications/Utilities/Terminal)
  2. Type this line in:



    sudo nano /private/etc/hosts

  3. You will be requested to type your password in.
  4. The terminal window will then show any current host settings. Now add the line you want. Use arrows keys to position flashing cursor where you want.



    EG: 127.0.0.1  mydomain.com.au
  5. Once done, hit Control+O to save. Then press Enter.
  6. Now exit Terminal - Control+X.

You can also download a "Hosts" application that will install and be available from System Preferences. However this will not work for all OSX versions. Terminal is a better way to go.

For more advanced users, you can use "sudo vim".

To flsuh DNS in OSX 10.5 + use "dscacheutil -flushcache" - for OSX 10.4 and below, use "lookupd -flushcache".

NOTE: Had a few questions on this, if you add domain without "www" - you need to check in browser without the www as well.

Friday, March 8, 2013

How to remove file extension via .htaccess

If you want to "tidy up" you site, this is a nifty little tip.
BUT - it should really be done for new sites, else you will have to add 301 redirects.


Further, if you don't know what htaccess is, you probably shouldn't be doing anything like this.

EG for PHP

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

The .htaccess file needs to go in the same directory as the file as it works from the top down. If you have the .htaccess in a certain directory, it will affect any other sub files/folders within it.

To add / at the end

Replace the last line of code:
RewriteRule ^(.*)$ $1.php

With these lines:
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

NOTE: You should use absolute paths, rather than a relative ones for images, CSS etc.
Also, avoid creating an edge case from using folder names the same as file names within it... else you will need to add an exception.