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

    
Rewriting Subdomains with .htaccess
What's wrong with my code?
JakeLD




msg:1514622
 7:03 pm on Sep 26, 2003 (gmt 0)

Hi,

I want to rewrite my subdomain to appear as my top level domain.

I don't want a redirect. I just want a link that is currently seen as [sub.domain.com...] to actually be seen as [domain.com...] or [domain.com...] when a page prints out.

Why is my code not working?

RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.sub.domain.com$
RewriteRule ^(.*)$ [domain.com...]

Thanks

 

jdMorgan




msg:1514623
 1:49 am on Sep 27, 2003 (gmt 0)

JakeLD,

Something is missing from this equation. Given that this .htaccess file is located in the web root of your site, then it can and will do nothing. That's because in order for the RewriteRule to be processed, the request has to be delivered to the directory where the .htaccess code resides. If that is true, then you don't need to do anything, i.e. you don't need mod_rewrite code for this, because you are already in the correct directory.

Also, there are two forms to a RewriteRule. One is a redirect and uses the basic form:

RewriteRule ^somepath$ http://somedomain [R,L]

This can be either a 301 or 302 redirect. It defaults (as shown) to a 302. Use [R=301,L] to force a 301-Moved Permanently, and [R=302,L] for a 302-Moved Temporarily if you like to have your code "self-documenting" for later reference.

The second form is something like this:

RewriteRule ^somepath$ /someotherpath [L]

This is not a redirect, it is simply a path substitution. Note that the substitution path is local - a URL beginning with [somedomain...] cannot be used.

Now, if you make your RewriteRule compliant with the requirements of the second form -- no [somedomain,...] it may be clearer why your rule appears to not work:

RewriteCond %{HTTP_HOST} ^(www¦sub)\.domain\.com$
RewriteRule ^(.*)$ / [L]

This will take a request for www.domain.com or sub.domain.com, and serve the index file of the current directory, which I suspect looks like it does nothing. Note that it will only work if either of the domains is requested without the trailing slash because you have end-anchored the pattern with "$".

After reviewing the above, you might want to re-state what you are trying to do. Because of the inconsistencies noted above, I can't really tell. It all depends on what directory the subdomain requests are delivered to now, and where you want to deliver them to using mod_rewrite. In all cases, the .htaccess file must be located in the directory where the visitor initially "lands" to "send" the visitor to the target directory.

Also, if you want to show a different domain name in the user's address bar, then that requires an external redirect.

Jim

<added>Note that I have used (www¦sub) to compress your two RewriteConds into one. If you wish to use this trick, you will have to edit the "¦" character to change it to a solid vertical pipe character from your keyboard. Posting on this board changes them to broken vertical pipes, which will not work in mod_rewrite.</added>

JakeLD




msg:1514624
 4:17 pm on Sep 27, 2003 (gmt 0)

Hi Jim,

Thanks for the reply. That gives me a much better understanding of mod_rewrite. I probably read Apache's tutorial 10 times and you made it much more clear. Let me explain my exact problem.

My web host allows me to create "Add On" domains. When doing this it adds an entry to httpd.conf that points my new domain (a different website) to a subdomain of the main website.

ex) newdomain.com points to subdomain.mainwebsite.com

It also creates a folder in the root directory (of the main website) that becomes the root directory of my new domain. I've added my .htaccess file here.

Where I run into problems is that I am running some .pl scripts out of the new cgi-bin (I can't edit these, I bought them). These scripts reference URLs in my new site but they are referenced as

subdomain.mainwebsite.com/file

Is this a setting on my hosts server that is causing this? Or can I use .htaccess to rewrite the subdomain reference with my new domain?

Thanks,

Jake

shady




msg:1514625
 1:28 am on Oct 28, 2003 (gmt 0)

There are two solutions that I can see. The following example assumes your username is mydomain and the subdomain is called subdomain!

1) If you have shell access, you can go to your main directory:
cd /home/mydomain/public_html
ls

You should see the directory for your subdomain listed
delete it:
rm -r subdomain

now create a symbolic link from your subdomain TO your main directory:

ln -s /home/mydomain/public_html subdomain

It will now work

OR if you do not have shell access ask your host to edit httpd.conf and change the DocumentRoot of the subdomain from /home/mydomain/public_html/subdomain to /home/mydomain/public_html

Hope this helps!
Shady

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