• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Redirecting non www. to www?

#1
I am trying to redirect my forum to http://www.example.com but it isn't working.I edited my .htaccess and added this code
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]
But still it didn't work.Need help.
 

Jake Bunce

XenForo moderator
Staff member
#2
Add the red code to the .htaccess file in your forum root. The second line must include the name of the forum directory. If your forum is in the web root then just remove the /forum part:

Code:
#	Mod_security can interfere with uploading of content such as attachments. If you
#	cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#	SecFilterEngine Off
#	SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
	RewriteEngine On

	RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$
	RewriteRule ^(.*)$ http://www.yoursite.com/forum/$1 [R=301,L]

	#	If you are having problems with the rewrite rules, remove the "#" from the
	#	line that begins "RewriteBase" below. You will also have to change the path
	#	of the rewrite to reflect the path to your XenForo installation.
	#RewriteBase /xenforo

	#	This line may be needed to enable WebDAV editing with PHP as a CGI.
	#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -l [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^.*$ - [NC,L]
	RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
	RewriteRule ^.*$ index.php [NC,L]
</IfModule>
 
#3
Add the red code to the .htaccess file in your forum root. The second line must include the name of the forum directory. If your forum is in the web root then just remove the /forum part:

Code:
# Mod_security can interfere with uploading of content such as attachments. If you
# cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
# SecFilterEngine Off
# SecFilterScanPOST Off
#</IfModule>
 
ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default
 
<IfModule mod_rewrite.c>
RewriteEngine On
 
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$
 RewriteRule ^(.*)$ http://www.yoursite.com/forum/$1 [R=301,L]
 
# If you are having problems with the rewrite rules, remove the "#" from the
# line that begins "RewriteBase" below. You will also have to change the path
# of the rewrite to reflect the path to your XenForo installation.
#RewriteBase /xenforo
 
# This line may be needed to enable WebDAV editing with PHP as a CGI.
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
 
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Nope,it still hasn't worked.
 

Robby

Well-known member
#6
Add the red code to the .htaccess file in your forum root. The second line must include the name of the forum directory. If your forum is in the web root then just remove the /forum part:

Code:
# Mod_security can interfere with uploading of content such as attachments. If you
# cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
# SecFilterEngine Off
# SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$
RewriteRule ^(.*)$ http://www.yoursite.com/forum/$1 [R=301,L]

# If you are having problems with the rewrite rules, remove the "#" from the
# line that begins "RewriteBase" below. You will also have to change the path
# of the rewrite to reflect the path to your XenForo installation.
#RewriteBase /xenforo

# This line may be needed to enable WebDAV editing with PHP as a CGI.
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
It doesn't work for me, can you take a look plz: forums.hidemychat.com
 
#8
Hi,

if i would like to rewrite to non-www.

whats the diff between using [NC]
Code:
Code:
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
and without [NC] ?
Code:
Code:
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
Also, what does the QSA in [R=301,L,QSA] do?
 
Last edited:

lazy llama

Well-known member
#11
The info about QSA can be found on the same page that Brogan linked to.
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:
Code:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
 
#14
Code:
    RewriteCond %{HTTP_HOST} !^main.com$ [NC]
    RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
The above code works fantastically. However I just added a Addon Domain (e.g. addon.com), uploading the addon.com website files to public_html/addon

Problem with the above code now is that whenever someone accesses addon.com, it gets redirected to main.com/addon. Is there a way to prevent this? I found out the only way to prevent this is get rid of the above code. But when I do that, I cannot redirect www. to non-www.

Lastly, to prevent duplicates, these 4 entry points to the addon-domain needs to be redirected back to the main domain.
e.g.
addon.main.com
www.addon.main.com
main.com/addon
www.main.com/addon

All 4 has to redirect to main.com


Anyone know how to achieve all this through .htaccess ?

Many thanks!
 

lazy llama

Well-known member
#15
Code:
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.main.com$'"
Rewrite ^(.*)$ http://main.com/$1 [R=301,L]
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.addon.com$'"
Rewrite ^(.*)$ http://addon.com/$1 [R=301,L]
For the final set of redirects, some are covered by the above but you would also need to add this:
Code:
RewriteCond %{HTTP_HOST} ^main.com$
RewriteCond %{REQUEST_URI} ^/addon/$
Rewrite ^(.*)$ http://main.com/ [R=301,L]
 
Last edited:
#16
Code:
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.main.com$'"
Rewrite ^(.*)$ http://main.com/$1 [R=301,L]
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.addon.com$'"
Rewrite ^(.*)$ http://addon.com/$1 [R=301,L]
For the final set of redirects, some are covered by the above but you would also need to add this:
Code:
RewriteCond %{HTTP_HOST} ^main.com$
RewriteCond %{REQUEST_URI} ^/addon/$
Rewrite ^(.*)$ http://main.com/ [R=301,L]
Thanks for the prompt reply!

Just to confirm, I shall replace the previous .htaccess edits
Code:
RewriteCond %{HTTP_HOST} !^main.com$ [NC]
RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
with this:
Code:
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.main.com$'"
Rewrite ^(.*)$ http://main.com/$1 [R=301,L]
RewriteCond expr " %{HTTP_HOST} -strcmatch '*.addon.com$'"
Rewrite ^(.*)$ http://addon.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^main.com$
RewriteCond %{REQUEST_URI} ^/addon/$
Rewrite ^(.*)$ http://main.com/ [R=301,L]
?
 

lazy llama

Well-known member
#17
I think so, yes. Take a back copy of the existing .htaccess in case you need to put it back.
Not been able to test it myself as I no longer use Apache, but it looks like it should work.

Ideally instead of doing this in .htaccess, it should be done using multiple <virtualhost> directives in the Apache config, which makes it simpler as you use the virtual host ServerName or ServerAlias to match the different domains and then only apply the re-write rules that apply to that domain. But I'm guessing you only have access to the .htaccess files.
 
#18
I think so, yes. Take a back copy of the existing .htaccess in case you need to put it back.
Not been able to test it myself as I no longer use Apache, but it looks like it should work.

Ideally instead of doing this in .htaccess, it should be done using multiple <virtualhost> directives in the Apache config, which makes it simpler as you use the virtual host ServerName or ServerAlias to match the different domains and then only apply the re-write rules that apply to that domain. But I'm guessing you only have access to the .htaccess files.
Just tried it and it gave an Internal Server Error :censored:

hmmm
 
#20
Okay, let's try it step by step.

Replace the original rewrite with
Code:
RewriteCond %{HTTP_HOST} !^main.com$ [NC]
RewriteCond %{HTTP_HOST} !^addon.com$ [NC]
RewriteRule ^(.*)$ http://main.com/$1 [R=301,L]
That should redirect only things which don't match http://main.com or http://addon.com
Thanks.

Just tried these. Works as you mentioned.

Main.com URLs are good. And,

www.addon.main.com
addon.main.com
www.main.com/addon

--> all these 3 gets redirected to main.com/addon/


Whats left is to ensure

www.addon.com does not get redirected to
and addon.com
points to add.com
and not get redirected to
main.com/addon/

also

www.addon.main.com
addon.main.com
www.main.com/addon

redirects to main.com
 
Last edited: