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

    
Rewrite puzzle - Any Apache experts out there?
Gremlin098




msg:3657656
 6:35 pm on May 23, 2008 (gmt 0)

I have two domains hosted in the same place - domain.com and seconddomain.com. I only want seconddomain.com to work if there is a subfolder after it, otherwise I want to rewrite it to domain.com. I also want to rewrite www.domain.com to just domain.com. The following htaccess is functional, but the www-to-non-www rewrite adds a second slash to the end of the domain (http://domain.com//) and a call for a subfolder with no trailing slash produces a 404 which, for some reason, is not handled by my errordocument redirect (I get my host error page instead, but file calls do redirect to my error page). Am I trying to do too much at once or is the code just plain wrong?

# If URL is seconddomain.com but no subfolder is requested, redirect to domain.com.
RewriteCond %{HTTP_HOST} seconddomain.com
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} \..+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

# rewrite from www to non-www.domain.com
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

# add a trailing slash to any subfolder requests without one
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1/ [L,R=301]

ErrorDocument 404 /error404.htm
ErrorDocument 400 /error400.htm

[edited by: jdMorgan at 3:38 am (utc) on May 26, 2008]
[edit reason] De-linked [/edit]

 

Gremlin098




msg:3657658
 6:36 pm on May 23, 2008 (gmt 0)

Sorry, also does the double slash matter? I was afraid it might be seen by search engines as duplicate content.

jdMorgan




msg:3657806
 9:09 pm on May 23, 2008 (gmt 0)

If this code is located in .htaccess, and the www-to-non-www is adding a second slash, this indicates a server configuration error -- Specifically, DocumentRoot for you server is defined with a trailing slash, which is an error. You should contact your host to fix this first.

You may be able to fix the ErrorDocument problem by adding a second RewriteCond to your first rule:

RewriteCond %{REQUEST_URI} !^/error

This will by-pass the rule if an error document is requested, instead of trying to redirect error document requests to the subdomain/subfolder.

Also, change the RewriteRule substitution URL to use the non-www domain!

There are several othe problems in this code. Get the DocumentRoot problem fixed, and then let's proceed with the rest.

Jim

Gremlin098




msg:3657835
 10:02 pm on May 23, 2008 (gmt 0)

Thanks Jim. I was afraid it might be the server config. I'll ask the host to take a look at it and post again when they fix it. I appreciate the assist.

jdMorgan




msg:3657890
 11:59 pm on May 23, 2008 (gmt 0)

If your DocumentRoot is indeed defined with a trailing slash (as evidenced by the fact that you shouldn't be getting two slashes as the result of a RewriteRule), refer them to the DocumentRoot documentation [httpd.apache.org]:

There appears to be a bug in mod_dir which causes problems when the DocumentRoot has a trailing slash (i.e., "DocumentRoot /usr/web/") so please avoid that.

Jim

g1smd




msg:3658849
 12:04 am on May 26, 2008 (gmt 0)

*** # rewrite from www to non-www.domain.com ***

You don't have a rewrite in the following code. You have a redirect only.

Gremlin098




msg:3658902
 2:29 am on May 26, 2008 (gmt 0)

Maybe I'm using the wrong term. I'm definitely an htaccess newbie. The following redirects from www.domain.com to just domain.com using a rewrite rule. Is there a better way to do it?

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ [%1...] [R=301,NC,L]

jdMorgan




msg:3658921
 3:36 am on May 26, 2008 (gmt 0)

Some of the characters in your RewriteRule really don't do anything in this case. The RewriteRule line could be reduced to

RewriteRule (.*) http://%1/$1 [R=301,L]

without changing the way it works.

Other than that, the post above was simply pointing out that this rule invokes a client redirect, and not a server-internal rewrite. Since mod_rewrite and regular expression patterns are so picky about every little character being correct, those of us who deal with it a lot like to use precise terminology to make things as clear and correct as possible... :)

Jim

Gremlin098




msg:3660106
 5:52 pm on May 27, 2008 (gmt 0)

Thanks Jim. I narrowed down the double slash problem from my first message. It is related to a Joomla SEO component I'm using called sh404SEF which apparently has a function to add a trailing slash to any error in hopes of finding the page before going to ErrorDocument. I'm sure that makes it impossible to troubleshoot without extensive work. The double slash is annoying but I love the component so, unless it creates duplicate search engine content, I'm inclined to leave it be. Do you happen to know if search engines will see 'domain.com//folder/' and 'domain.com/folder/' as duplicates?

g1smd




msg:3660128
 6:11 pm on May 27, 2008 (gmt 0)

They certainly do see them as Duplicate.

I am not impressed that a so-called "Search Engine Friendly" component makes such a problem for the site.

Then again I have a very low opinion of everything Joomla! It's a poorly implemented pile of junk.

A URL that ends with a trailing / is a folder, or a call for the index file within a folder. It is itself not directly a file.

Gremlin098




msg:3660135
 6:23 pm on May 27, 2008 (gmt 0)

Can't say I disagree, but then you get what you pay for :-)

I may have to find a different SEO component though. It defeats the purpose if its creating duplicate content.

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