Redirect Using Mod_rewrite

admin

Administrator
Staff member
have my blog setup on a subdomain that is accessible by going to <a href="http://blog.domain.com" target="_blank">http://blog.domain.com</a>, but not <a href="http://www.blog.domain.com" target="_blank">http://www.blog.domain.com</a>. However, I would like for users to still be able to type <a href="http://www.blog.domain.com" target="_blank">http://www.blog.domain.com</a> and get the blog instead of the friendly 404 page.<br /><br />I was thinking that the best way to do this would be to use mod_rewrite. I searched around the forums and found some threads with code. I modified one snippet, but it didn't work when testing it.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->RewriteEngine On<br />RewriteCond %{HTTP_HOST} !^blog\.domain\.com <br />RewriteRule ^(.*)$ http://blog.domain.com/$1 [L,R]<!--c2--></div><!--ec2--><br /><br />must be something that I messed up with my code.<br /><br />Does anyone know how I could improve that code or have code that I could use? Any help would be greatly appreciated.<!--content-->
bump !<!--content-->
First, I read somewhere that subdomains have to be typed in without the <!-- w --><a class="postlink" href="http://www..">www..</a><!-- w -->. and I never verified that for certain. But up until now, that's what I've assumed.<br /><br />You asked about mod rewrite. Before I came to TCH, I wanted to use Apache Force Type but my old host ran windows or it wouldn't work for some reason so I worked my way through with mod rewrite and successfully did it. It wasn't easy and that's my only experience with mod rewrite.<br /><br />I found this<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->RewriteCond %{HTTP_HOST}  !^fully\.qualified\.domain\.name [NC]RewriteCond %{HTTP_HOST}  !^$<br />RewriteCond %{SERVER_PORT} !^80$<br />RewriteRule ^/(.*)        <a href="http://fully.qualified.domain.name:%{SERVER_PORT}/$1" target="_blank">http://fully.qualified.domain.name:%{SERVE...R_PORT}/$1</a> [L,R]<br />RewriteCond %{HTTP_HOST}  !^fully\.qualified\.domain\.name [NC]<br />RewriteCond %{HTTP_HOST}  !^$<br />RewriteRule ^/(.*)        <a href="http://fully.qualified.domain.name/$1" target="_blank">http://fully.qualified.domain.name/$1</a> [L,R]<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />Here<br /><br /><a href="http://httpd.apache.org/docs/misc/rewriteguide.html" target="_blank">http://httpd.apache.org/docs/misc/rewriteguide.html</a><br /><br />So you might want to check that out. <br /><br />I tried to Google your question and didn't come up with much. Sorry.<!--content-->
OK that completely confused me <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/blink.gif" style="vertical-align:middle" emoid=":blink:" border="0" alt="blink.gif" /> Ummm without all the jargon is there some easy way I can get people who type <a href="http://www.blog.****" target="_blank">http://www.blog.****</a> to be redirected to <a href="http://blog.****" target="_blank">http://blog.****</a> ?<!--content-->
Found this in Google... this looks like the answer you are looking for<br /><br />ht*p://isp-lists.isp-planet.com/isp-webhosting/0203/msg00551.html<br /><br />I wrote the above url so there wouldn't be a link... I think you know to replace the * with a t.<br /><br /><br />I'm sorry the Apache Mod Rewrite info was confusing... but this stuff isn't easy. Please be aware that if you start giving the server instructions to modify your urls without really knowing what's going on, then you run the risk of something going haywire in a big way.<!--content-->
thanks rite i'll have a look at the link . Yeah I put that code (form my first post) into my htaccess and everything got screwed. My whole subdomain crashed but was easily fixed by removing that line ! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/blink.gif" style="vertical-align:middle" emoid=":blink:" border="0" alt="blink.gif" /><!--content-->
Rite had a look at the code on that page. Wow i'm more confused. SHould I attempt this what does that code do ?<!--content-->
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->RewriteEngine on<br />## Ignore <!-- w --><a class="postlink" href="http://www.****">www.****</a><!-- w --> and ****<br />        RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]<br />        RewriteCond %{HTTP_HOST} !^yourdomain\.com [NC]<br />## A directory with the name of the subdomain must exist<br />        RewriteCond %{DOCUMENT_ROOT}/%1 -d<br />## Add the requested hostname to the URI<br />## [C] means that the next Rewrite Rules uses this<br />        RewriteRule ^(.+) %{HTTP_HOST}/$1 [C]<br />## Translate abc.****/foo to ****/abc/foo<br />        RewriteRule ^([a-z0-9A-Z]+)\.yourdomain\.com/?(.*)$http://www.******/$1/$2 [L]<!--c2--></div><!--ec2--><ul><li>create a text file</li><li>name it whatever you want</li><li>Add the above code to it</li><li>replace 'yourdomain' with your domain (leave out www and dot whatever)</li><li>save the file</li><li>ftp the file to your server in your public_html directory</li><li>rename the file .htaccess</li></ul><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->My whole subdomain crashed but was easily fixed by removing that line !<!--QuoteEnd--></div><!--QuoteEEnd--><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->SHould I attempt this<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />Seems like you've seen what can happen and you survived. It probably wouldn't be a bad idea to back up your site... database and all. But that's just good policy whenever making a major change site wide.<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->what does that code do ?<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />Dunno. Didn't write it.<!--content-->
it looks like this code just redirects abc.**** to ****/abc look at this line <!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->## Translate abc.****/foo to ****/abc/foo<!--QuoteEnd--></div><!--QuoteEEnd--> sorry but that's not what i wanted. I don't want to attempt something before I'm sure that I really want to do it because if that's what the code does then I'm not putting it into my htaccess sorry for the trouble !<!--content-->
I'm not sure about this, but I am not sure what you want can be done. <br /><br />Currently, www already is a subdomain that points to your public_html folder. So what you're looking for is a way to change where a subdomain points to, correct? (You want it to point to '/public_html/blog' instead of '/public_html'.)<br /><br />Anyone know how the server knows to send www to public_html?? I would think that is where the change would need to be made (or at least, that's what htaccess would have to override, if possible).<br /><br />*edit* after reading the link surefire gave, it does indeed claim to do what you want. I'm not entirely sure what it's doing either, but that is what it claims to do. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
<!--QuoteBegin-arvind2100+Sep 29 2003, 08:16 AM--><div class='quotetop'>QUOTE(arvind2100 @ Sep 29 2003, 08:16 AM)</div><div class='quotemain'><!--QuoteEBegin-->have my blog setup on a subdomain that is accessible by going to <a href="http://blog.domain.com" target="_blank">http://blog.domain.com</a>, but not <a href="http://www.blog.domain.com" target="_blank">http://www.blog.domain.com</a>. However, I would like for users to still be able to type <a href="http://www.blog.domain.com" target="_blank">http://www.blog.domain.com</a> and get the blog instead of the friendly 404 page.<!--QuoteEnd--></div><!--QuoteEEnd--><br />Honestly, <!-- w --><a class="postlink" href="http://www.subdomain.domain.org">www.subdomain.domain.org</a><!-- w --> looks so ugly, I am inclined to show the user the friendly 404 (along with a site map and search box) <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> <br /><br />Another thing is, rewrite rules make use of regular expressions, and bad expressions can slow down the server and delay page display.<!--content-->
 
Back
Top