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

Apache Web Server Forum

Canonical Issue
How To Fix

5+ Year Member

Msg#: 4232451 posted 5:18 pm on Nov 19, 2010 (gmt 0)

How do I fix the following issue:

You can get to my site by typing www.capitalsolutionsonline.net/
and www.capitalsolutionsonline.net/index AND www.capitalsolutionsonline.net/index.html

I'm already have the following code to direct the non www version to the www:

RewriteEngine on
rewritecond %{http_host} ^capitalsolutionsonline.net [nc]
rewriterule ^(.*)$ [capitalsolutionsonline.net...] [r=301,nc]

Question is what code to I add or what do I do to fix the other issues.

Thank you in advance.



5+ Year Member

Msg#: 4232451 posted 11:40 am on Nov 23, 2010 (gmt 0)

Something like

RewriteRule ^(.*)/index(\.html|/)?$ http://www.example.com/$1 [R=301,L]

Note I didn't test this and wrote it off the top of my head, may need some tweaking and you may need a rewritecondition.

Two points:

Use example.com - your links won't get turned into URLs on here and it's easier to work with.

Follow coding standards - "rewritecond" is wrong - it's "RewriteCond" - as is r=301 - should be R=301. It may seem pedantic but it matters much more with Apache directives and mod_rewrite than other types of code. Yeah, the code still works on your server with the wrong case, but if you ever move servers you could find all this code suddenly stops working.


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

Msg#: 4232451 posted 9:51 pm on Nov 30, 2010 (gmt 0)

RewriteEngine on
# Externally redirect direct client requests for named index pages
# in any directory to default index URL for that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.capitalsolutionsonline.net/$1 [R=301,L]
# Externally redirect all non-blank non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.capitalsolutionsonline\.net)?$
RewriteRule ^(.*)$ http://www.capitalsolutionsonline.net/$1 [R=301,L]

Note that these redirect rules are ordered from most-specific URL-paths and conditions to least-specific. Both rules should precede any and all internal (URL-to-filepath) rewrite rules.

The "complexity" of the first rule is needed to prevent an 'infinite' rewrite/redirect loop due to interaction with the DirectoryIndex directive which currently rewrites requests for the URL-path "/" to the filepath "/index.html". It is therefore necessary to check to be sure that the request for "/index.html" is coming straight from the HTTP client (e.g a browser), and is not the result of the DirectoryIndex rewrite.


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