Welcome to WebmasterWorld Guest from 34.237.76.91

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Rewrite hostname, no redirect

strip www. prefix

     
2:41 am on Jan 30, 2007 (gmt 0)

New User

10+ Year Member

joined:May 2, 2005
posts:11
votes: 0


Is it possible to rewrite the hostname without sending a redirect?
I'm trying to strip www prefixes for all hosts but I want apache to handle it internally.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^/(.*)$ http://%1/$1 <-- don't want a redirect, just let apache use the new vhost

The reason I want this is because I've got ~ 10,000 vhosts set up with vhost_alias. Each vhost is a symlink to the user's homedir, ie

/var/www/vhosts/example.com  --> /var/wwwroot/user0001
/var/www/vhosts/www.example.com --> /var/wwwroot/user0001

I could use half the number of symlinks if I could just get rid of the www symlink. :)

Thanks,
Martin

3:16 am on Jan 30, 2007 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


> Is it possible to rewrite the hostname without sending a redirect?

No, the hostname can only be changed by using a redirect.

If (as in your code example) you include "http://x.y.z/" in the RewriteRule substitution, and do not specify [R=nnn] in the RewriteRule flag, then Apache will generate a 302-Moved Temporarily redirect response by default.

You might consider using an alternate form of the VirtualDocumentRoot directive rather than the default (%0) form. For example:


UseCanonicalName Off
VirtualDocumentRoot /usr/local/apache/vhosts/%0.-2.%0.-1

would map URL requests for either www.example.com or example.com to filepath /usr/local/apache/vhosts/example.com without all the added symlink complication.

See Apache mod_vhost_alias [httpd.apache.org] for more information.

Jim

4:39 am on Jan 30, 2007 (gmt 0)

New User

10+ Year Member

joined:May 2, 2005
posts:11
votes: 0


Thanks Jim.

I assume what you meant was

VirtualDocumentRoot /usr/local/apache/vhosts/%-2.0.%-1.0

But that won't work with [www.]any.number.of.subdomains.com, such as [www.]example.co.uk

I guess what I really need then is something like

if (host begins with "www.")
{
VirtualDocumentRoot /usr/local/apache/vhosts/%1+
}
else
{
VirtualDocumentRoot /usr/local/apache/vhosts/%0
}

Is this possible?

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members