Forum Moderators: phranque
domain1.com
sub1.domain.com
domain2.com (which is a subdomain sub2.domain.com but setup the way you can access it online like domain2.com)
By a historic reason I use sub1.domain.com as my main website. So domain1.com points to sub1.domain.com
I wanted to unify my sub1.domain.com so if anyone tries to access it as sub1.domain.com it will permanently redirect to www.sub1.domain.com
I'm not good with mod_rewrite but I came up with this:
[2][b]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www.sub1.domain.com$ [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^.*/(.*) http://www.sub1.domain.com/$1 [R=301,L]
[/b][/2] Well, it works, but now I have 2 other problems:
1) how can I permanently redirect my domain.com to sub1.domain.com?
2) my second subdomain (sub2.domain.com), which was accessible as domain2.com now redirects to www.sub1.domain.com
Any help is appreciated!
[edited by: jdMorgan at 3:06 pm (utc) on April 13, 2007]
[edit reason] De-linked [/edit]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(sub1\.)?domain1\.com [NC]
RewriteRule ^([^/]*/)+(.*)$ http://www.sub1.domain1.com/$2 [R=301,L]
Jim
Now my sub1.domain1.com is unified and domain2.com works fine.
The only thing that is still not working is when I type
[domain1.com...] - page not found
but when I type [domain1.com...] - it executes index.html
In both cases I need to permanently redirect it to [sub1.domain1.com...] to be search engines and bots friendly and to no brake the existing unification for sub1.domain1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(sub1\.)?mydomain\.com [NC]
RewriteRule ^([^/]*/)+(.*)$ [sub1.mydomain.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule (.*) [sub1.mydomain.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule (.*) [sub1.mydomain.com...] [R=301,L]
One thing to mention, since I updated .htaccess with suggested code by jdMorgan I see many errors in the log like this:
File does not exist: /home/myname/public_html/sub1/adjs.php
adjs.php is a part of Openads but it is located in /home/myname/public_html/sub1/openads/adjs.php
I checked the code, there are no places that might have a bad URL.
Any ideas?
File does not exist: /home/myname/public_html/sub1/adjs.php
I note that your RewriteRule pattern discards any path information preceding the 'filename' in the requested URL. Because you did not mention this function, or the reason for it, I have retained it, but used a more-efficient regular-expressions pattern.
Your second rule is redundant and unneeded, because the "sub1" in the first rule is optional, as specified by the "(sub1)?" pattern. Based on your rules and the problem you report, you can replace all three of your rules with this one:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.¦sub1\.)?example\.com [NC]
RewriteRule (.*) http://www.sub1.example.com/$1 [R=301,L]
Important: Replace the broken pipe "¦" character in the RewriteCond above with a solid pipe character before use; Posting on this forum modifies the pipe character.
Jim