WebmasterHub.net Forums

Website & Application Design & Development, Programming, SEO, SharePoint Development & Administration




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Apache mod_rewrite (.htaccess) - Rewrite or Redirect Request
PostPosted: Thu Jan 28, 2010 6:09 am 
Contributer

Joined: Mon Aug 31, 2009 9:18 am
Posts: 27
Apache mod_rewrite : rewrite or redirect if page not found using specific criteria

The following article provides details and examples for using Apach mod_rewrite (.htaccess) to rewrite or redirect requests based on specific criteria, such as the inclusion of "www" before the TLD (Top Level Domain), and rewriting requests to load a page with query string paramaters taken from compnents of the request.

Apache mod_rewrite (.htaccess) - Rewrite or Redirect Request URLs if page not found, or no www.

Mod_rewrite examples:

Redirect to include www in the host:
- Redirect "domain.com" to "www.domain.com"
Code:
RewriteBase /
RewriteCond %{HTTP_HOST} !^www(.*)
RewriteRule ^(.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R]


Rewrite request components to use as Query String paramaters for a specific page:
- Rewrite "www.domain.com/display/2009/10/" to "www.domain.com/display.php?y=2009&m=10"
Code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^display/([0-9][0-9][0-9][0-9])/([0-9][0-9])(/?) display.php?y=$1&m=$2 [L]


Redirect to to site root if page not found:
- Redirect 404 error (Page not Found) to root of domain ( "www.domain.com/non-existent-page" redirects to "www.domain.com/" )
Code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://%{HTTP_HOST} [R]


For more information, and more detailed examples of how and when to use Apache mod_rewrite to rewrite and redirect requests, see Apache mod_rewrite (.htaccess) - Rewrite or Redirect Request URLs if page not found, or no www.


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  



SEO Tools & Resources Programming & Web Resources SharePoint Resources
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
DVGFX2 By: Matt