Forum Moderators: phranque

Message Too Old, No Replies

Redirect www and non-www to same folder

         

hoopz

7:52 am on Jan 10, 2010 (gmt 0)

10+ Year Member



Using .htaccess, I'm trying to redirect example.com and www.example.com to www.example.com/m/, but I get the following error message: Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Here's the .htaccess entry:

Options +FollowSymlinks
rewriteengine on
rewritecond %{HTTP_HOST} ^www.example.com$ [NC,OR]
rewritecond %{HTTP_HOST} ^example.com$ [NC]
rewriterule ^ http://www.example.com/m/ [R=301,L]

Any ideas what I'm doing wrong?

Much appreciated,
H

jdMorgan

4:51 pm on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've created an infinite loop, as a request for / is redirected to /m/, then that's redirected to /m/m/, then to /m/m/m/, ... /m/m/m/m/m/m/m etc.

Fixing that and cleaning up some code redundancy gives:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/m/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^ http://www.example.com/m/ [R=301,L]

This redirects any request for any resource in the example.com or www.example.com domain to the root of subdirectory "/m" -- that is, any URL-path-part in the request will be discarded. Take care that you've got a mechanism in place to detect and handle requests for such 'required' files as /robots.txt and /sitemap.xml, or you may destroy the ranking of your (mobile?) site...

Alternatively, you may wish to create and use a back-reference to the requested URL-path, and simply prepend "/m" to it, so that it does not get discarded.

Also, I would question whether you really want to 'expose' the /m subdirectory to the client. A 'silent' internal server filepath rewrite might be more appropriate. Of course, this depends on details not posted and perhaps obscured by the 'example' domain names. :)

Jim

[edited by: jdMorgan at 3:01 am (utc) on Jan. 11, 2010]

g1smd

12:30 am on Jan 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(typo) siteamp.xml => sitemap.xml

jdMorgan

3:03 am on Jan 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Never host on a porum when you're in a furry...

Jim