homepage Welcome to WebmasterWorld Guest from
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

This 38 message thread spans 2 pages: < < 38 ( 1 [2]     
redirecting directory index to the root for multiple subdomains

Msg#: 4585301 posted 10:21 am on Jun 18, 2013 (gmt 0)


I have seen previous topics re. directing index.html to / , but must admit that I do find it a bit confusing, as there are so many suggestions. Some say add code to the .htaccess file, others to the system config (not sure exactly what that is). Anyway, here's my problem. I mistakenly linked about 12,000 pages as index.html and want to get rid of the problem permanently (I have corrected my links now), redirecting anything that is index.html to the directory before. Here are some examples:

http://www.example.com/index.html to:

http://www.example.com/europe/index.html to:

http://www.example.com/europe/england/index.html to:

http://www.example.com/europe/england/somerset/index.html to:

http://www.example.com/europe/england/somerset/bath/index.html to:

http://www.blog.example.com/index.html to:

There are lots of different folders to redirect, so I was hoping to do something simple to tackle every url that is /index.html. Is there something simple that I can do to achieve this, rather than adding a huge chunk of code? Your suggestions (and patience!) are much appreciated. :)

Many thanks,


[edited by: phranque at 11:13 am (utc) on Jun 18, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]



Msg#: 4585301 posted 10:31 am on Jun 19, 2013 (gmt 0)

OK, noted. Someone earlier on the post told me to do that, but I will make sure I fix it, when we decide what to do. Thanks.


WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 4585301 posted 10:37 am on Jun 19, 2013 (gmt 0)

GET /index.html HTTP/1.1
Host: www.europe.example.com

HTTP/1.1 301 Moved Permanently
Location: http://www.example.com/countries/europe/
GET /countries/europe/ HTTP/1.1
Host: www.example.com

HTTP/1.1 301 Moved Permanently
Location: http://www.example.com/europe/countries/europe/
GET /europe/countries/europe/ HTTP/1.1
Host: www.example.com

HTTP/1.1 404 Not Found

something on your server is adding that path information to the url and your next task is to find out where that is occurring.
it is happening in one of the following places:
- a .htaccess file in your document root directory or a subdirectory
- a server configuration file
- your CMS or the script that is generating these responses

look for "countries" in those places and report your findings.

note that www.europe.example.com and other hostnames may be in a separate virtual host configuration (and therefore possibly resides in another document root directory) or could possibly be hosted on a separate server.


Msg#: 4585301 posted 10:43 am on Jun 19, 2013 (gmt 0)

Thanks. There is nothing else in the .htaccess file and the pages are static. How do I check my server configuration file? Whereabouts would I find that exactly?


Msg#: 4585301 posted 3:53 pm on Jun 19, 2013 (gmt 0)

I'm back. :) I had to reproduce the issue on my local machine in order to find a working solution.

The problem occurs because rewrite rules match against filesystem paths, not URL paths. That's why the filesystem path to your subdomains gets added. There are a couple ways we can solve this.

Option 1)

In each if your subdomains, create an htaccess with just this one line:

RewriteOptions Inherit

This way, the subdomain will still run all the same rewrite code, but it will do so from the context of its own directory, so there won't be any path prefix.

Option 2)

We could change the rewrite to match against the URI instead of the filesystem path. That means we'll have to do most of the heavy lifting in a rewrite condition.

RewriteCond %{REQUEST_URI} ^/(.+/)?index\.html?$
RewriteRule ^ /%1 [R=301,L]


Msg#: 4585301 posted 4:07 pm on Jun 19, 2013 (gmt 0)

Thanks Dideved for going to so much trouble. That was SO kind of you.

I tried Option 2, in the main .htaccess file, and everything seems to work perfectly. How fantastic! Thanks again. I can't believe we've managed to find such an elegant solution. Wow. I'm so chuffed with that. Wow! :) :) :)


Msg#: 4585301 posted 12:47 am on Jun 20, 2013 (gmt 0)

you are correct in your understanding and you support my point.
two 301s lose as much PR as following a link through two pages.
the lesson is to link internally to the canonical url and fix externally referred non-canonical requests in a single redirect.

Also, I want to say that I've re-examined Matt's words, and I think, phranque, that you're right and I'm wrong, and I'll definitely have to include this bit of information in the future.

Though, I think ease of maintenance is still a factor, and for the specific situation in this thread, I think that's the factor that should win out. The OP didn't specify how many subdomains he has, but it sounds like there's one for each region, country and city, such as europe, england, and bath. If we omit the hostname from the rewrite rule, then that one rule can work for every subdomain. But if instead we were to hardcode a hostname, then we may need to copy-paste that rewrite rule for each subdomain.


Msg#: 4585301 posted 6:08 am on Jun 20, 2013 (gmt 0)

Thanks. It's quite a big site, about 10 years old). There's about 1,000 old subdomains now. We restructured the whole thing as the subdomains were being treated separately in so many ways (e.g. Google Plus), and we wanted an 'authority site' where there was a collection of 'pages' rather than 'subdomains'. The result so far has been better than I could have hoped for, so far!

Thanks again for everything. This was a neat solution.


WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 4585301 posted 7:27 am on Jun 20, 2013 (gmt 0)

Change all Redirect directives to use RewriteRule.

Put the index rule before the non-www/www rule in each case.

This 38 message thread spans 2 pages: < < 38 ( 1 [2]
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