Support ⇒ Troubleshootings :: Archives ⇒ How to redirect non-www to www :: Archived ⇒ Community Forums ⇒ CPG Dragonfly™ CMS
Forum IndexTroubleshootings

Archived ⇒ How to redirect non-www to www


I have read that it is possible to redirect non-www links to www using .htaccess file, i.e:
RewriteEngine On RewriteCond %{HTTP_HOST} ^dragonflycms\.org$ [NC] RewriteRule ^(.*)$ http://www.dragonflycms.org/$1 [R=301,L]
But it is not working with my site. Where exactly should I put this code in my .htaccess? OR is there any other way of redirection?

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


Please consider doing it the other way round: no-www.org/

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


DJ Maze wrote
Please consider doing it the other way round: no-www.org/
DJ Maze, I could not understand your point. Can you explain it in more detail?

I went to no-www.org, there is nothing but the code in the first post.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


I mean
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] is also not working. I have tried this one also before opening this thread.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


then .htaccess rewrite is probably turned off for your account

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


mod_rewrite is enabled. I can use friendly URLs like www.uzislam.com/Forums.html or uzislam.com/Forums.html but I can't force no-www to www or vice-verse. I put the redirect code to .htaccess but it's not working....

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


Then you probably are using the wrong order.
Just show your FULL .htaccess file

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


Here it is, it is the native Dragonfly .htaccess file with additionally redirect code:

# CPG Dragonfly CMS # Copyright (c) 2004-2006 by CPG-Nuke Dev Team, dragonflycms.org # Released under the GNU GPL version 2 or any later version # $Source: /cvs/html/.htaccess,v $ # $Revision: 9.18 $ # $Author: nanocaiordo $ # $Date: 2007/01/17 03:24:44 $ # Remove the pound sign on these 3 for production sites # if your server doesn't allow it then a Error 500 is given # php_flag display_errors off # php_value error_reporting 0 # php_flag register_globals 0 # flood protection # deny most common except .php <FilesMatch "\.(inc|tpl|h|ihtml|sql|ini|conf|bin|spd|theme|module)$"> deny from all </FilesMatch> # disable access to config.php and .ht* from a browser <FilesMatch "^(config\.php|\.ht)"> Deny from all </FilesMatch> <FilesMatch "error\.(php|gif)"> allow from all </FilesMatch> # if you use LEO, mod_rewrite is necessary <IfModule mod_rewrite.c> RewriteEngine On # Check for Santy Worms and redirect them to a fail page #------------------------------------------------------------------- # Variant -1 # uncomment if you dont use LWP # RewriteCond %{HTTP_USER_AGENT} ^LWP [NC,OR] # Variant -2 RewriteCond %{REQUEST_URI} ^visualcoders [NC,OR] # Variant -3 RewriteCond %{QUERY_STRING} rush=([^&]+) [NC,OR] # Variant -4 RewriteCond %{HTTP:x-moz} ^prefetch [NC,OR] RewriteCond %{X-moz} ^prefetch [NC,OR] # block local file, sql and remote attacks RewriteCond %{QUERY_STRING} =../ [NC,OR] RewriteCond %{QUERY_STRING} "%20UNION" [NC,OR] RewriteCond %{QUERY_STRING} =http:// [NC] # deny them RewriteRule ^.*$ - [F] #------------------------------------------------------------------- RewriteCond %{REQUEST_FILENAME} -f [NC,OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.*)$ - [L] # if you use LEO and CPG-Nuke is installed in a sub-directory like '/html', # remove that # before RewriteBase and rename /html to the path of the sub-directory # RewriteBase /html # RewriteRule ^index\.html /index.php RewriteRule ^([a-zA-Z0-9_=+-]+)(/|\.html)$ index=$1 [L,S=5] RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)(/|\.html)$ index=$1&file=$2 [L,S=4] RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ index=$1&file=$2 [L,S=3] RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(.*)(/|\.html)$ index=$1&file=$2&$3 [L,S=1] RewriteRule ^([a-zA-Z0-9_]+)/(.*)(/|\.html)$ index=$1&file=index&$2 [L] RewriteRule ^index=(.*[^/])/(.*) index=$1&$2 [N,L] RewriteRule ^index=(.*) index.php?name=$1 [L] </IfModule> # use custom error pages if you wish ErrorDocument 400 /error.php ErrorDocument 401 /error.php ErrorDocument 403 /error.php ErrorDocument 404 /error.php ErrorDocument 500 /error.php # disallow index viewing (like ftp) of directory # Remove # for production sites # Options -Indexes # for hosts that don't allow the above, we won't give people anything to look at <IfModule mod_autoindex.c> IndexIgnore * </IfModule> AddDefaultCharset utf-8 # RewriteEngine On - This is commented out because we already have this command on line 33 RewriteCond %{HTTP_HOST} ^uzislam\.com$ [NC] RewriteRule ^(.*)$ http://www.uzislam.com/$1 [R=301,L]

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


Just what i thought, programming is not your cup of coffee.

Try to understand the following change:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^uzislam\.com$ [NC] RewriteRule ^(.*)$ http://www.uzislam.com/$1 [R=301,L] # Check for Santy Worms and redirect them to a fail page
۞ ۩

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


OK, I understand. redirect code must be between <IfModule mod_rewrite.c> and </IfModule>. Thanks DJ Maze, I will give it a try.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


It worked, thanks! :)

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


Not only that, [L] means "Last rule".
Since the mod rewrite of Dragonfly comes first, it never gets to your rule.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


Hmm... But there are several [L]'s in mod rewrite rule, how they co-work?

Moreover I put the redirect rule just after <IfModule mod_rewrite.c> (i.e. as a first rule), does that mean all other rules are being ignored?

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.37/5.0.27/5.2.1/9.2.1


If a rule matches then it is the "Last rule"
else it just keeps going to the next rule.

Yes Df uses L a lot of times, this is on purpose to prevent the N rule getting into an infinite loop.
This is high end stuff so you should read up on apache documentation.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial

All times are UTC