Organizational Research By

Surprising Reserch Topic

how to wrtie rule VirtualHost Apache - non-www is not redirect



asked Sep 22, 2013 in APACHE by rajesh
0 votes
92 views



Related Hot Questions

1 Answer

0 votes
I have a strange issue. Cause I am using the "exact" code on many different domains, however, this domain does not redirect the non-www to the www subdomain. Any thoughts? the DNS is setup correctly. using CNAME. Identical setup across all domains I'm using, this one doesn't work, I'm not using htacess, only running everything through virtualhost.conf on apache, since its a much faster way.
 
  DocumentRoot /var/www/html/mydomain.com
  ServerName mydomain.com
 
  RewriteEngine on
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
  RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]
 
  DocumentRoot /var/www/html/mydomain.com
  ServerName www.mydomain.com
 
  RewriteEngine on
 
  # remove trailing slash
  RewriteRule ^/(.*)/$ /$1 [R=301]
 
  # remove /index.php
  RewriteCond $1 !^(index\.php|images|assets|3rdparty|robots\.txt)
  RewriteRule ^/(.*)$ /index.php/$1 [QSA]
Also, to note, the domains use codeigniter. However that isn't an issue, because the domain itself isn't redirecting once hitting apache, it stays on the non-www and the www version works. but then I have 2 versions of the website.
 
UPDATE:
 
DocumentRoot /var/www/html/mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
 
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]
 
# remove trailing slash
RewriteRule ^/(.*)/$ /$1 [R=301]
 
# template 15 fix
RewriteRule ^/15/(.*)$ /index.php/$1 [QSA]
 
# remove /index.php
RewriteCond $1 !^(index\.php|images|assets|3rdparty|robots\.txt)
RewriteRule ^/(.*)$ /index.php/$1 [QSA]
(all of this works, except the non-www redirect) and mod_rewrite is enabled, as I'm using it, but I can not find the rewrite_module anywhere if that matters.
 
Found a Fix:
 
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/(.*)$ http://www.mydomain.com/$1 [R=301,L]
Doing it this way (adding in the domain) worked perfectly.
answered Sep 22, 2013 by rajesh

...