Removing query string question mark
Assuming your httpd.conf is configured in such a manor to allow .htaccess to override Apache directives we will start by creating a file named .htaccess
This .htaccess file is simply just a text file which is parsed and interpreted by Apache. You do not need to specifically let Apache know about this file as directories are scanned for these files.
Removing the query string question mark
First we enter the following code into our .htaccess file. This will make sure the mod_rewrite module is available.
# Rewrite URLs
<ifmodule mod_rewrite.c>
</ifmodule>Then we enabled the rewrite engine.
# Rewrite URLs
<ifmodule mod_rewrite.c>
RewriteEngine on
</ifmodule>Now we can begin with a few rewrite conditions indicated with RewriteCond. The first uses the ${REQUEST_FILENAME} variable and makes sure that it is not a filename, and the second does the same but for directories.
So now that our URL is not a file, and is not a directory, we can rewrite it using the RewriteRule directive. The pattern ^(.*)$ matches all of the request text and $1 in the substitution of index.php?q=$1 simply backreferences what we captured with the pattern mentioned above.
Each RewriteRule may optionally contain flags, in this case we have [L] indicating that it is the rewrite process should stop after this, encase we have more RewriteRules below.
# Rewrite URLs
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L]
</ifmodule>Now lets test it out! you should now have an index.php file located at example.com/index.php along with our .htaccess file in the same directory. Then when you type example.com/just/a/test you should be able to print_r($_GET) and see that the array member of 'q' now contains 'just/a/test', which can be used for further processing within your framework.
Perhaps we now need the use of query strings as well such as example.com/just/a/test?name=vision this will not work unless we use the Query String Append flag which is simply QSA.
# Rewrite URLs
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</ifmodule>Practical Mod Rewrite Examples
Locale Prefix
Say we want to separate a local prefix at the HTTP server level, such as example.com/en/path/to/page
We would use the following rewrite rule pattern. ([a-z]{2}) simply matches lower case characters a through z, and only will match two of them. The remaining portion of the pattern has not changed, we have only appended locale=$1 since it was the first group captured, and q=$2 as the remaining portion was captured second.
There we have it! $_GET['locale'] is now available via PHP.
# Rewrite URLs
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})\/(.*)$ misc.php?q=$2&locale=$1 [L,QSA]
</ifmodule>Disguising Dynamic Scripts
The following rule will rewrite .html files as .php
# Rewrite URLs
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [L,QSA]
</ifmodule>Additional Mod Rewrite Information
For more information regarding Apache 2.x URL rewriting via mod_rewrite visit:
- http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
- http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Delicious
Digg
StumbleUpon
Reddit
Facebook
Comments
thanks for this info, it is easy to follow and the sample runs well.
thanks for the site author/s and sponsor. keep it up guys! more coding power and GOD bless to all of Us!