Use mod_rewrite to redirect from example.com/dir to www.example.com/dir

Posted by kavoir.com on Stack Overflow See other posts from Stack Overflow or by kavoir.com
Published on 2010-12-26T12:28:37Z Indexed on 2010/12/26 12:54 UTC
Read the original article Hit count: 420

Filed under:
|
|
|

Assume / is the document root of my domain example.com.

/.htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^golfcoursesd\.com$ [NC]
RewriteRule ^(.*)$ http://www.golfcoursesd.com/$1 [R=301,L]

/dir/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dir/index.php [L]
</IfModule>

I know how to redirect example.com/dir to www.example.com/dir, because /.htaccess does the very job.

However, the trick here is that I have to keep /dir/.htaccess to serve up virtual directories (such as /dir/state/AK/35827/ which aren't actual directories) if you know what I mean.

Problem is, if I keep /dir/.htaccess, a request of:

http://example.com/dir/state/AK/35827/

DOES NOT redirect to:

http://www.example.com/dir/state/AK/35827/

as would:

http://example.com/

redirect to:

http://www.example.com/

Not sure if I made it clear.

Basically, how to make http://example.com/dir/state/AK/35827/ correctly redirect to http://www.example.com/dir/state/AK/35827/ AND I can serve virtual URLs?

© Stack Overflow or respective owner

Related posts about apache

Related posts about .htaccess