Welcome to WebmasterWorld Guest from 54.167.177.207

Forum Moderators: Ocean10000 & incrediBILL & phranque

Rewrite puzzle - Any Apache experts out there?

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

5+ Year Member



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]

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

5+ Year Member



Sorry, also does the double slash matter? I was afraid it might be seen by search engines as duplicate content.
9:09 pm on May 23, 2008 (gmt 0)

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



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

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

5+ Year Member



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.
11:59 pm on May 23, 2008 (gmt 0)

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



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

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

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



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

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

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

5+ Year Member



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]

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

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



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

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

5+ Year Member



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?
6:11 pm on May 27, 2008 (gmt 0)

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



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.

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

5+ Year Member



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.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month