views:

1004

answers:

6

I have a mortgage calculator site that doesn't seem to redirect from mookal.com to www.mookal.com My apache config is as follows:

RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.mookal.com [nc]
RewriteRule ^(.*)$ http://www.mookal.com/$1 [r=301,nc]

what am I missing? thanks!

+4  A: 

Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

And then you'll have another <VirtualHost> section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).

Greg Hewgill
Didn't know that one. Thanks!
cherouvim
A: 

Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST}  ^mookal.com$       [NC]
RewriteRule ^(.*)         http://www.mookal.com$1  [R=301]
Mark Ursino
+2  A: 
<VirtualHost *:80>
    ServerAlias example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
cherouvim
+1  A: 
RewriteCond %{HTTP_HOST} ^!mookal.com$ [NC]
RewriteRule ^(.*)$ http://www.mookal.com/$1 [R=301,L]

This starts with the HTTP_HOST variable, which contains just the domain name portion of the incoming URL (example.com). Assuming the domain name does not contain a www. and matches your domain name exactly, then the RewriteRule comes into play. The pattern ^(.*)$ will match everything in the REQUEST_URI, which is the resource requested in the HTTP request (foo/blah/index.html). It stores this in a backreference, which is then used to rewrite the URL with the new domain name (one that starts with www).

[NC] indicates case-insensitive pattern matching, [R=301] indicates an external redirect using code 301 (resource moved permanently), and [L] stops all further rewriting, and redirects immediately.

Curtis Tasker
+1  A: 

To remove www from your url website use this code on .htaccess

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

To force www in your website url use this code on .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]

Were "YourSite.com" you must replace for your url.

+1  A: 

I ran this...

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

I need this to be universal for 25+ domains on our new server, so this directive is in my virtual.conf file in a <Directory> tag. (dir is parent to all docroots)

I had to do a bit of a hack on the rewrite rule though, as the full docroot was being carried through on the pattern match, despite what http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html says about it only being stuff after the host and port.

Andrew Deal