.htaccess non-www to www

yoghurtfarmer

Well-known member
Noticed my rewrite rule is no longer working and would like some help in fixing it :). Does it have anything to do with the L flag?

Please and thanks.

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data|js|styles|install) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

RewriteCond %{HTTP_HOST} ^url\.com$ [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [R=301,L]
 
still, try to type facebook.com into your browser, it shows the domain including "www"
:)
That's the browser doing that, to appease the masses. In chrome, if you type it without the www, you never see the www, and rightly so. ;) The exception to that is when you copy/paste the URL, because in some cases you might actually need the www (coding HTML, other browsers, etc.) wherever you're pasting it to.
 
I use this rule for my XF - Plesk/Centos.
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^domain.com
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data|js|styles|install) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>
 
I use this rule for my XF - Plesk/Centos.
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data|js|styles|install) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>

Same you have to put the http_host rule before the xf rewrites
 
What's the fascination/craze with removing www. from URLs now?

Is it just to be different? For neatness?

When I was at vB I either had to redirect to www or get facebook connection errors on non www connections
(XF spoils its users with a Fantastic Product and this sort of thing is just not an issue)

The other reason I redirect to www is because google analytics sees www and non www as different domains

I don't yet know what your policy is on bumping a 4mth old thread but I was pointed here from a new thread
*cringe and hit post reply*
 
I have several subdomains. Which one of these rewrite rules enforces www.url.com only? I need it to redirect from url.com to www.url.com

In the context of XF's default .htaccess file, add the red code:

Rich (BB 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>
 
In the context of XF's default .htaccess file, add the red code:

Rich (BB 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>
Thanks. I replaced yoursite.com with my site's name but the problem is that if you go to http://mysite.com you are not logged in, but if you go to http://www.mysite.com you are logged in.

How do I enforce it to only direct members to http://www.mysite.com instead of letting them go to http://mysite.com?
 
In the context of XF's default .htaccess file, add the red code:

Rich (BB 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>
Hey Jake this actually worked except the problem is that none of my forums are on in the /forum folder. They are in the root.
 
Thanks. I replaced yoursite.com with my site's name but the problem is that if you go to http://mysite.com you are not logged in, but if you go to http://www.mysite.com you are logged in.

How do I enforce it to only direct members to http://www.mysite.com instead of letting them go to http://mysite.com?

You will need to login again if you never logged in on that specific subdomain. Cookies don't cross subdomains by default.

The rewrite rules I posted will force users to access the forum using that specific subdomain.

Hey Jake this actually worked except the problem is that none of my forums are on in the /forum folder. They are in the root.

Remove the /forum part.
 
You will need to login again if you never logged in on that specific subdomain. Cookies don't cross subdomains by default.

The rewrite rules I posted will force users to access the forum using that specific subdomain.



Remove the /forum part.
Thanks Jake. I tried that but it's not saving my updated settings. I think there might be issues with the server. Contacting my host now.
 
Did you update your board URL?

Admin CP -> Home -> Options -> Basic Board Information -> Board URL

I can take a look if you give me FTP access.
It's actually working now. I was updating my htaccess from my friends house. I drove home and when I looked at both sites they seem to be working fine now.
 
Top Bottom