Forum Moderators: phranque

Message Too Old, No Replies

Domain unified but stuck with rewrite rule for subdomains

kind of not usual situation here, help is needed

         

mnemonik23

2:52 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



What I have:

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]

mnemonik23

2:54 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



I knew I will screw it up :(

everywhere it says sub1.domain.com it should be sub1.domain1.com

jdMorgan

3:05 pm on Apr 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understood your question and the correction in your second post:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(sub1\.)?domain1\.com [NC]
RewriteRule ^([^/]*/)+(.*)$ http://www.sub1.domain1.com/$2 [R=301,L]

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.

Jim

mnemonik23

3:22 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



jdMorgan, it worked!

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

mnemonik23

6:06 am on Apr 14, 2007 (gmt 0)

10+ Year Member



Ok, I figured this part and now my rewrites look like this (sorry for the auto links, I don't know how to remove them yet but they are not real):

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?

jdMorgan

5:44 pm on Apr 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



File does not exist: /home/myname/public_html/sub1/adjs.php

Yes, I mentioned this potential problem above:
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]

This means "If the subdomain is 'www' or 'sub1' or blank, then redirect all requests to 'www.sub1.example.com'"

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

mnemonik23

10:26 pm on Apr 14, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Almost worked!

When I try [sub1.domain.com...] it redirects me to [sub1.domain.com...]

jdMorgan

10:51 pm on Apr 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the code won't do that. You have some other code (possibly inserted by your 'control panel' in httpd.conf) that is doing a rewrite, which is then being redirected.

Jim

mnemonik23

11:20 pm on Apr 14, 2007 (gmt 0)

10+ Year Member



Only these

ErrorDocument 403 /403.jsp
ErrorDocument 404 /404.jsp
ErrorDocument 401 /401.jsp
ErrorDocument 500 /500.jsp

and a few IP deny...

jdMorgan

2:59 pm on Apr 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I wrote above, the code that is interfering is in httpd.conf, and was most likely put there by using your "Control Panel."

Jim

mnemonik23

5:46 pm on Apr 15, 2007 (gmt 0)

10+ Year Member



Ah, and I do not have access to httpd.conf :(

Thanks for your help Jim! I'll try to wotk this out with my hosting provider.