Forum Moderators: phranque

Message Too Old, No Replies

Subdomain 301 Redirect

404 error

         

imweb

2:28 pm on Oct 16, 2005 (gmt 0)

10+ Year Member



I need help to solve this problem. I want to redirect subdomain from www.example.com to sub.example.com. There is a folder /sub/ at doc root. So I did rewrite:


//.....other rewrite rule [R=301,L]
RewriteCond %{HTTP_POST} ^www.example.com [NC]
RewriteRule (.*) http://sub.example.com/$1 [R=301,L]

It produces 404 error code. In browser address it showed http://sub.example.com/sub/

Can anyone help fix this problem? Thanks.

[edited by: jdMorgan at 2:57 pm (utc) on Oct. 16, 2005]
[edit reason] Example.com [/edit]

jdMorgan

2:54 pm on Oct 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you may be using a "control panel" to define the subdirectory in which files for "www.sub.example.com" are stored. In that case, you'll need to do the same thing to define a subdirectory "/sub" in which to store files for sub.example.com.

Jim

[edited by: jdMorgan at 2:56 pm (utc) on Oct. 16, 2005]

cws3di

2:55 pm on Oct 16, 2005 (gmt 0)

10+ Year Member



I'm notand expert, and I'm not sure this is what is causing the 404, but here is my suggestion: Try a backslash \ in front of the dots . like this:

RewriteCond %{HTTP_POST} ^www\.sub\.example\.com [NC]
RewriteRule (.*) http://sub\.example\.com/$1 [R=301,L]

[edited by: jdMorgan at 2:57 pm (utc) on Oct. 16, 2005]
[edit reason] Example.com [/edit]

cws3di

2:58 pm on Oct 16, 2005 (gmt 0)

10+ Year Member



oops, the expert was posting while I was typing. Follow his advice, not mine :-)

jdMorgan

6:07 pm on Oct 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cws3di,

You were correct to point out that the literal periods in regex patterns should be escaped:


RewriteCond %{HTTP_POST} ^www\.sub\.example\.com [NC]
RewriteRule (.*) http://sub.example.com/$1 [R=301,L]

However, since the sustitution URL is not a regex pattern, there is no need to escape characters in it unless you wish to insert a literal "%" or "$" character. For example:

RewriteRule ^items/cigar_box_20.html /show_product.php?item=cigar_box&cost[b]=\$20[/b] [L]

Here, the backslash prevents mod_rewrite from looking for a back-reference of $2 and trying to insert its (undefined) value into the substitution URL.

Jim

imweb

1:48 am on Oct 17, 2005 (gmt 0)

10+ Year Member



Thanks Jim.

I fixed this problem by creating rewrite rule in .httaccess in /sub/ directory instead of / directory.