xampp htaccess on mac

I searched quite a bit for this topic, but did not find any solid help where I can setup htaccess file locally on xampp on a site that is siting in htdocs folder. The following are the tips to setup htaccess properly on mac

  1. HTACCESS need to be put in htdoc folder. In previous version of XAMPP this would usually be installed in /users/{username}/sites/. In the latest version 1.8.3, this folder is in /Applicatoiin/Xampp/xamppfiles/htdoc/ folder.
  2. The name of the file should be .htaccess, not htaccess
  3. You can edit the file in any editor. I used textmate. Sometime basic editor may not work because in /Applications/ folder you need an editor which can handle permissions. Ordinary editors did not work for me.
  4. You do not need to edit anything else, provided it is all setup rightly (as of Exampp 1.8.3)
  5. A sample rewrite file looks like this, at least for me
    RewriteEngine On
    
    RewriteBase /demo
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^review/(.*)$ review.php?id=$1 [NC,QSA,L]

    Here the /demo/ folder is sitting in /htdocs/. /htdocs/ is the root folder.

I have seen one problem so far. The images do not load after using .htaccess. I have to change path of each image to full path. For example it used to be ./images/image1.jpg, but now I have to change it to /demo/images/image1.jpg. I will try to find a fix for that problem and update it here.

Solution: The reason the images do not load is probably because of the fact that my site is setup in htdocs folder and notas an independent site (Virtual Host). One setup as independent site, this problem will go away.

While writing this article, I got help from this SO question

HTACCESS in Virtual Host on XAMPP Mac

If you are setting up htaccess inside a virtual hosted site, the above step apply with the following modifications.

  1. .HTACCESS (not htaccess) file needs to sit the directory where the site is hosted. In this case not not to be in htdoc folder but inside the root directory of the website.
  2. You may need to add the following line to HTTPD-VHOST.conf file
    <VirtualHost *:80>
        ServerAdmin /Projects/Git/mysite.co
        DocumentRoot "/Projects/Git/mysite.co"
        ServerName mysite.git
    	ServerAlias mysite.git
        ErrorLog "/Projects/Git/mysite.co/logs/example.com-error_log"
        CustomLog "/Projects/Git/mysite.co/logs/example.com-access_log" common
    	<Directory "/Projects/Git/mysite.co">
    	        Allow from all
    		AllowOverride All     <----- This line is important
    	        Require all granted
    	</Directory>
    </VirtualHost>

How to test HTACCESS

The following htaccess can be used to test if your htaccess is being hit at all. Basically any url inside will be pointed to index.html file. For exmaple
http://www.helloworld.com
http://www.helloworld.com/just-a-test
http://www.hellowworld.com/test.php

All of the above will be pointed to index.html, if it exists.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index2.html [NC,QSA,L]
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s