.htaccess tutorial

htaccess Elite


Redirect www to subdomain on internal links

Ask your mod_rewrite and Redirection questions here, and get answers!

Redirect www to subdomain on internal links

New postby KaworuNagisa » 01 Feb 2008 01:17

I'd like to try redirecting/rewriting internal links to the www subdomain to go to a different subdomain. For example, let's say on my page I have a link written like this:

<a href="http://www.mydomain.com/">My Home Page</a>

Can I do something in the .htaccess file that will send the user instead to "http://sub.mydomain.com/" when the link is clicked? Thanks in advance for the help.

KN
KaworuNagisa
 
Posts: 2
Joined: 31 Jan 2008 23:55

Re: Redirect www to subdomain on internal links

New postby mod_rewrite » 01 Feb 2008 03:16

KaworuNagisa wrote:<a href="http://www.mydomain.com/">My Home Page</a>

Can I do something in the .htaccess file that will send the user instead to "http://sub.mydomain.com/" when the link is clicked? Thanks in advance for the help.

KN


Yes KN, just edit your root .htaccess file and add this solution based on require the www in .htaccess

Code: Select all
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.mydomain.com/$1 [R=301,L]
mod_rewrite
 
Posts: 102
Joined: 30 Oct 2006 19:55

New postby KaworuNagisa » 01 Feb 2008 05:00

Ok I kinda see what you're doing there but it didn't work out for me. Perhaps I should mention that the link to the http://www.mydomain.com is on a page that is actually on sub.mydomain.com. So I'm clicking the link while on the subdomain that I want the link to go to, only the link is coded to go to the www subdomain. Am I missing something? Is there a way to do this at all?
KaworuNagisa
 
Posts: 2
Joined: 31 Jan 2008 23:55

New postby mod_rewrite » 01 Feb 2008 19:17

.htaccess can only control requests to the server, it can't control outgoing requests, for that you need javascript.

Unless you can modify the .htaccess file that is in the root folder for the domain http://www.site.com you can't do it.

If you do, please provide information on your setup and any errors you get when doing this code (which works)
mod_rewrite
 
Posts: 102
Joined: 30 Oct 2006 19:55


Return to Redirect or Rewrite Questions