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

Remove superflous directory which has same name as domain

 4:01 am on Sep 25, 2012 (gmt 0)

I want to redirect http://example.com/example.com/index.htm to http://example.com/index.htm

I use htaccess to host multiple parked domains on one account...

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^/*example\.com/ example.com%{REQUEST_URI} [L]

I place all the domain files in the directory "example.com" but I screwed up somehow and now Google sometimes points to the directory "example.com"

I tried the following...

RewriteCond %{THE_REQUEST} ^example\.com/(.*)\ HTTP/
RewriteRule ^example\.com/(.*)$ http://www.example.com/$1 [R=301,L]

But it does not work... I really have no idea what I am doing, so help would be appreciated.

[edited by: phranque at 6:04 am (utc) on Sep 25, 2012]
[edit reason] use example.com please [/edit]



 6:15 am on Sep 25, 2012 (gmt 0)

Looks like you haven't been around in a while-- unless you're a very devoted lurker-- so start by reading the thread about using example.com. Look at your own post and you will see the point.

A RewriteRule can only see the "body" of an URL. Anything involving domains and/or queries has to go into a RewriteCond.

But before you do too much work, check with your host. A lot of them will do this kind of routine redirection for you, so you don't have to mess with htaccess if it alarms you.


 6:21 am on Sep 25, 2012 (gmt 0)

it does not work...

what was the status code?

THE_REQUEST is going to begin with the HTTP Request method such as GET or POST so your RewriteCond will always be false.
in your case the RewriteCond is probably unnecessary.

what is your canonical hostname?
in your problem statement you use example.com yet you use www.example.com in your RewriteRule.


 6:39 am on Sep 25, 2012 (gmt 0)

^(www\.)? in the condition with ^[A-Z]{3,9}\_ where _ is a space should get you closer.

You should not be redirecting to a named index.html file. The canonical URL for the site root ends with a trailing slash.


 7:00 am on Sep 25, 2012 (gmt 0)

Oops. My bad. I didn't read your question closely enough or I'd have seen that the problem is duplication. Somewhere in the past you goofed in a redirect and came out with something like


Have I got that right?

If so, you don't need a condition at all. The element "example.com" will never be part of an URL-- other than in the "domain" slot, where the RewriteRule can't see it. And unlike "index.html" or equivalent it will never be supplied by anything operating behind the scenes, so you don't need to look at THE_REQUEST. When you redirect

(that is, "www.example.com/example.com/" possibly ending in directory slash followed by an explicit "index.html")


http://www.example.com/ [R=301,L]

the "index.html" part will quietly disappear if it was there, and your domain name-- the real one-- will get canonicalized. So that's three birds with one stone ;)


 6:43 am on Sep 26, 2012 (gmt 0)

Thank you phranque & lucy24, I will remove the condition and report back.

I've been letting my site run on auto pilot, but now it has 10,000 visitors a day I figured it is time to earn some adsense revenue off it.


 9:46 pm on Sep 26, 2012 (gmt 0)

This works, for everything except my domain name. If I try a random example...

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

Then http://www.example.com/fred.com/puzzles is rewritten as http://www.example.com/puzzles

If I try my domain name

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

Then http://www.example.com/example.com/puzzles remains unchanged... Help?

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