How To Fix 403 Forbidden Error from htaccess File

As a web developer, you will likely need to make rewrites for clean URLs and produce an htaccess file so you can create ModRewrite rules.

If you encounter a 403 Forbidden Error after creating the htaccess file. This happens because even though the htaccess file itself may have the right permissions, it is possible the web server is not explicitly allowing the rewrites for that directory.

To check for this you should take a look at the webserver error log. The webserver error log may be in different locations depending on your operating system. On Mac OS X it’s in /var/log/apache2/error_log, on most Linux boxes it’s in /var/log/httpd/error_log

for example, you can view the last few errors in the error log by using the tail follow command in the linux command line like this:

promt $> tail -f /var/log/apache2/error_log

If you see an error like this:

[Fri Feb 27 09:20:28 2015] [error] [client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /Users/Leasure/Sites/zfSkeleton/public/

Then you can fix the issue by adding the following line to the top of the .htaccess file:

Options +FollowSymLinks

So that it may look something like this ( the other code should be code specific to your own set up). The important thing to know is that by adding Options +FollowSymLinks you explicitly allowing the rewrites.

Options +FollowSymLinks
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] 

If you are comfortable editing your Apache httpd.conf file, you can add the Options +FollowSymLinks to the directory directive like this:

<VirtualHost *:80>
ServerName skeleton.loc
DocumentRoot "/Users/Leasure/Sites/zfSkeleton/public"
   <Directory "/Users/Leasure/Sites/zfSkeleton/public">
       Options FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>
</VirtualHost>

Leave a Reply

Your email address will not be published. Required fields are marked *