Forum Moderators: phranque

Message Too Old, No Replies

Subdomain Resolves to www When Trailing Slash Missing

         

egomaniac

7:43 pm on Apr 2, 2002 (gmt 0)

10+ Year Member



I am using mod rewrite to implement my subdomains. My webhosting company implements TLDs by using mod rewrite to redirect the TLD into a subdirectory of my server space. I just put up my first subdomain, and it works great except for one problem..

When a user enters a URL in the address window by hand without the trailing slash, the browser's address window displays his location incorrectly.

CORRECT BEHAVIOR:

If the user enters:
[word.domain.com...]
or
[word.domain.com...]

Then he is taken to the "/blue/index.htm" page, and his address window correctly shows:
[word.domain.com...]

INCORRECT BEHAVIOR:

If the user does not include the trailing slash (hand entry into address window):
[word.domain.com...]

Then his address window will resolve to the following:
[domain.com...]

Here are the mod-rewrite rules from my .htaccess file (note - I barely understand this; I just hacked this together from reading a support doc my webhosting company offered, and then reading up on some Apache stuff):

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for word.domain.com
RewriteCond %{HTTP_HOST} word.domain.com$
RewriteCond %{REQUEST_URI} !/word/
RewriteRule ^(.*)$ /word/$1

Anyone know how to fix this so that a URL entered without the trailing slash will resolve as follows?
[word.domain.com...]

Brett_Tabke

8:56 am on Apr 3, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month




The interaction between modrewrite and the server, is surprisingly tricky. Although, I probably can't give the perfect answer, I'm sure I could hack it out. I'd start with:

On the last rewrite rule

RewriteRule ^(.*)$ /word/$1

Change to

RewriteRule ^(.*)$ /word/$1/

bird

11:32 am on Apr 3, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if mod_rewrite is the culprit here. The trailing slash for directory names is added by mod_dir [httpd.apache.org]. The problem seems to be that it uses the default site name for this purpose. This probably happens before the cited RewriteRule comes into effect, as a very similar rule that I have in place for testing purposes works perfectly fine.

Unfortunately, I don't see a direct way to influence which hostname mod_dir uses for this redirect. It must be some other configuration detail of the server that makes it work as expected on my server but not on yours.

Using "RewriteRule ^(.*)$ /word/$1/" might actually cause more problems than it solves, as the "$1" may well be a file name instead of a directory, and you don't really want to add the slash in that case.

dhdweb

1:26 am on Apr 4, 2002 (gmt 0)

10+ Year Member



I have the very same problem! Anyone else have any ideas?

P.S. adding the slash at the end of the last rewrite does cause some strange problems!

egomaniac

1:34 am on Apr 4, 2002 (gmt 0)

10+ Year Member



Brett & bird, thanks for the replies.

I tried changing the last rewrite rule to:

RewriteRule ^(.*)$ /word/$1/

...but it causes a 404 error.

I poked around the Apache documentation, and I tried adding the following:

UseCanonicalName off

...based on what I read here:

[httpd.apache.org...]

...but this caused every request to the server to return a "500 Internal Server Error" error code.

Anyone got any further ideas on this?

If I don't get this fixed, will it cause me any spidering problems with Google or any of the other SEs?

egomaniac

2:02 am on Apr 4, 2002 (gmt 0)

10+ Year Member



I just spoke to a tech at my webhosting company. He said that he has the same issue with his own personal website, and that there was no workaround that he could find for this problem.

If I were to setup separate IP numbers, that would solve the problem.

It doesn't seem that the spiders would have a problem with this, since they are following the correct syntax full-paths on mywebpages. The only time this issue comes up is when someone hand enters a URL, or trims the URL.

So I am going to hang loose with this defect, and I think I'll be fine.

Thanks for the discussion.

bird

2:48 am on Apr 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I were to setup separate IP numbers, that would solve the problem.

Which explains why it works for me.
Did I already reccommend to use IP based hosting in this thread? ;)

Key_Master

3:00 am on Apr 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

RewriteRule ^(.*)/([a-zA-Z]+)$ $1/$2/
RewriteRule ^/([a-zA-Z]+)$ /$1/

egomaniac

5:56 am on Apr 4, 2002 (gmt 0)

10+ Year Member



Hey Keymaster,

Thanks for the suggestion. I tried adding those lines, with no success. Perhaps I didn't add them correctly (keep in mind I am low-tech on this one).

Here's what I tried:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for word.domain.com
RewriteCond %{HTTP_HOST} word.domain.com $
RewriteCond %{REQUEST_URI} !/word/
RewriteRule ^(.*)/([a-zA-Z]+)$ $1/$2/
RewriteRule ^/([a-zA-Z]+)$ /$1/

What I did above is I swapped in your 2 lines for last RewriteRule line in my code.

Is this what you were suggesting?

Key_Master

6:55 am on Apr 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm no Rewrite expert either. ;)

This has got to work...

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for word.domain.com
RewriteCond %{HTTP_HOST} word.domain.com/$
RewriteCond %{REQUEST_URI} !word/
RewriteRule ^(.+[^/])$ word/$1/

egomaniac

3:18 pm on Apr 4, 2002 (gmt 0)

10+ Year Member



Hi Keymaster,

This one didn't work.

In testing your rule by leaving out the trailing slash, it appears to resolve by moving up one level in the directory heirarchy and adding the www host back in. The results of the test were a little screwy, partly due to my directory structure being a little hard to keep track of.

skeefy1

2:10 am on May 9, 2002 (gmt 0)



Check out the following guide, it has some good information that seems to be closely related:

[httpd.apache.org...]

guilley

7:24 pm on May 25, 2002 (gmt 0)



Here is one solution to the problem. This piece of code is be copied into the httpd.conf file :

RewriteEngine on
# Rename in HTTP mode:
RewriteCond %{HTTP_HOST} !^word.domain.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS} "off"
RewriteRule ^/(.*) [word.domain.com...] [L,R]
# Rename in HTTPS mode:
!^word.domain.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS} "on"
RewriteRule ^/(.*) [word.domain.com...] [L,R]