Wednesday, July 20, 2011

Working with .htaccess

What is .htaccess:
.htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. -Wikipedia

.htaccess is a way to modify the web server without editing the Master file.

In Apache the master file is called httpd.conf

How to create 404 and 403 pages:
404 pages appear when it doesn't exist (alternative to index.html in every directory on site) and 403 is a page exist but is forbidden to access.

In order for .htaccess to work you need to configure httpd.conf
AllowOveride must be set to All. See below. Remember its case sensitive. For some reason apache ships as "all" instead of a capitalized "All".

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All

What to put in .htaccess:
ErrorDocument 404 /yourerrorpage.html
ErrorDocument 403 /yourerrorpage.html

# Options -Indexes is what allows the 403 error to work
Options -Indexes


If you don't want to use .htaccess you can simply put
ErrorDocument 404 http:yourerrorpage.html
ErrorDocument 403 /yourerrorpage.html

Warning: Make sure you always restart Apache when changes are made to the httpd.conf because the changes won't be taken into effect until you do

You must edit httpd.conf at the server, you can't use ftp unless it's not secured. It should be located at /etc/apache2/httpd.conf



How to rewrite URLs?
To manipulate your links like transfroming ?name=page&URL's into /friendly/cute/links
you must activate rewrite_module in Apache.

Left click on the wamp icon then hover over "Apache" and go to "Apache Modules" and click on rewrite_module (or mod_rewrite) so it has a check. (or 500 Internal Server Error may throw)

or open httpd.conf and uncomment(remove #) the line below
LoadModule rewrite_module modules/mod_rewrite.so

Wait for Apache to restart.

In .htaccess insert this code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) mvc/index.php/$1

------or-------

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

More information:
http://corz.org/serv/tricks/htaccess2.php

Good MVC clean URL video


_________________________________________________
Ubuntu sever 11.10
Now the mod rewrite is a little different in the Ubuntu server. You have to create a "rewrite.load" symlink from /etc/apache2/mods-available to /etc/apache2/mods-enabled by running:
$ sudo a2enmod rewrite
then edit:
$ sudo nano /etc/apache2/sites-available/default
and make line 11 to AllowOverride None to AllowOverride All, see snippet below

<Directory /home/marko/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>


then restart apache:
$ sudo service apache2 restart

reference: http://www.techytalk.info/enable-apache-mod_rewrite-on-ubuntu-linux/


description: mod_rewrite wamp, .htaccess issue (500 - Internal Server Error), .htaccess apache mvc configuration 500, redirecting and rewriting

No comments:

Post a Comment