homepage Welcome to WebmasterWorld Guest from 50.19.172.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Please explain
troyid

10+ Year Member



 
Msg#: 3919238 posted 1:44 am on May 24, 2009 (gmt 0)

Can someone please explain what all these lines in my htaccess file do.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule .* - [F]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.example.com/$1 [R=301,L]

Thanks

Troy

[edited by: jdMorgan at 2:57 am (utc) on May 24, 2009]
[edit reason] example.com [/edit]

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3919238 posted 2:57 am on May 24, 2009 (gmt 0)

That code could use some improvements, even if it had included comments...

# Enable the rewriting engine
RewriteEngine On
#
# If the requested hostname starts with "example"<any character>"com" (case-insensitive)
RewriteCond %{HTTP_HOST} ^example.com [NC]
# externally redirect the request to the proper www subdomain, keeping the
# originally-requested URL-path (the so-called "filename" part of the URL)
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# If the requested hostname is not "www"<any character>example<any character>"com" (case-insensitive)
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
# return a 403-Forbidden response
RewriteRule .* - [F]
#
# If the client request line contains "/index<any character>html"
RewriteCond %{THE_REQUEST} ^.*/index.html
# redirect to "/" in the same directory of the www subdomain
RewriteRule ^(.*)index.html$ http://www.example.com/$1 [R=301,L]



This could be more properly and efficiently written as

# Enable the rewriting engine
RewriteEngine on
#
# If the requested hostname starts with "example.com" or "www.example.com" (case-insensitive)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
# but isn't exactly "www.example.com" (for example, if the "www." is missing, or
# if it contains uppercase characters, is an FQDN, or has a port number appended)
RewriteCond %{HTTP_HOST} !^www\.example\.com$
# externally redirect the request to the proper www subdomain, keeping the
# originally-requested URL-path (the so-called "filename" part of the URL)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Now if the requested hostname is not exactly "www.example.com" or blank
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
# return a 403-Forbidden response
RewriteRule ^ - [F]
#
# If the client request line contains "/index.html" as the requested URL-path
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*/index\.html([#?][^\ ]*)?\ HTTP/
# redirect to "/" in the same directory of the www subdomain
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

See the resources cited in our Apache Forum Charter and the threads in our Forum Library for more information -- Links to these resources are at the top of this page.

Jim

[edited by: jdMorgan at 2:58 am (utc) on May 24, 2009]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved