Forum Moderators: phranque
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
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]
The second form is something like this:
RewriteRule ^somepath$ /someotherpath [L]
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]
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>
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
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