Huge use .htaccess file, you MUST make change of apache http.conf file to enable mod_rewrite
apache config folder, edit http.conf file
find and change to AllowOverride All
open the file .http.conf
Find the line #LoadModule rewrite_module modules/mod_rewrite.so and remove the #
================================================
To enable mod_rewrite, I went through the below steps:
- Find and open the file .http.conf. This will be located in your Apache install folder. For me, the full path was C:Program Files (x86)Apache Software FoundationApache2.2conf
- Make a backup of http.conf which you can restore from, if anything were to go wrong
- Find the line #LoadModule rewrite_module modules/mod_rewrite.so and remove the hash ‘#’
- Locate the block within the directory tags as indicated below and change to:
Options All
AllowOverride All
Redirect All Requests To Index.php Using .htaccess
In one of my pet projects, I redirect all requests to index.php, which then decides what to do with it:
Simple Example
This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
This enables the rewrite engine:
RewriteEngine on
This checks for existing folders (-d) and files (-f):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
And this does the actual redirecting:
RewriteRule . index.php [L]
Extended Example
You can extend this to pass the requested path to the index.php file by modifying the RewriteRule to the following:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The ^(.*)$ part tells the rewrite module that we want to pass down the whole requested path as one parameter. The QSA part tells the module to append any query strings to the request. The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter. You can extend this even further by using regular expressions. For example:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request
http://yourhost.com/some/path/somewhere
will result in
http://yourhost.com/index.php?first=some&second=path/somewhere
This allows for some creative ways to do clean URLs.
Trouble Shooting
If it's not working, make sure that mod_rewrite is installed on Apache. On a unix system you can just do
sudo a2enmod rewrite
to achieve that.
========================
Using mod_rewrite to redirect all requests to a page
I always forget how to do this, so I thought I would write it down somewhere.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1
When in a .htaccess file (provided AllowOverrides is enabled) this will redirect all requests that don't match a physical file to
/index.php
, passing the request into the$_GET['request']
variable.
If you miss out the second line: you will get a 500 internal server error as your response because the request to
/index.php
would redirect to itself.
No comments:
Post a Comment