Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: htaccess - force www version of pages  (Read 668 times)

mihomes

  • Newbie
  • *
  • Karma: 2
  • Posts: 23
htaccess - force www version of pages
« on: October 19, 2012, 11:57:16 pm »

On all my websites I use the following in my htaccess file to always force the www version of the url in the browser :

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

This breaks when using snews in a location such as www.example.com/snews/.  Currently the default htaccess is :

Code: [Select]
<IfModule mod_php4.c>
    php_value session.use_trans_sid 0
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?category=$1 [L]

which reads... if the file exists then serve it.  If the directory does not exist then serve index.php.  How in the world can I go about using the 301 redirect to force all pages as www versions?
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: htaccess - force www version of pages
« Reply #1 on: October 20, 2012, 06:07:53 am »

There's a string missing under RewriteEngine On.
In the default package, the string is - #RewriteBase /sNews17

In order for the links to work OK when sNews is in a sub-directory in your domain root, you need to remove the #-sign to enable the string and - change the string to read RewriteBase /snews - with no slash on the end.
... This says the base (root) for snews is now the snews directory in the domain root.
... That way snews knows where it is and works OK.
... Do not change anything else in the default .htaccess file unless you really know .htaccess directives well.

There is no need to have www showing in URLs these days. WWW is redundant, as browsers find web pages without it.
What we're trying to achive with the .htaccess directives in the default .htaccess file is make the URLs as search engine friendly and not make them longer than they need to be.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

mihomes

  • Newbie
  • *
  • Karma: 2
  • Posts: 23
Re: htaccess - force www version of pages
« Reply #2 on: October 21, 2012, 06:19:26 pm »

Tried that and it doesn't change anything.  While you may not feel the need for it this is just something I have always done.  Technically, by spec, www and non-www are two different entities.  If you have ever used Google's Webmaster Tools this is why you first need to verify the non-www version of your site should you wish to add the www version.  Few other things regarding backlinks, dupe content, etc., etc.

Does it all matter?  Probably not these days.  Either way... I would still like to get this working and I can't understand why it would not at the moment.
Logged

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: htaccess - force www version of pages
« Reply #3 on: October 21, 2012, 06:26:41 pm »

1. What Keyrocks said

2. www can be (should be possible to) force through your webhost, either via a cpanel setting or the webhost doing it for you

3. There might be a way to force www within the context of the snews htaccess but, sorry, I have no input to offer on that. Personally I've never had the need for it so never bothered to find such a solution.
Logged

mihomes

  • Newbie
  • *
  • Karma: 2
  • Posts: 23
Re: htaccess - force www version of pages
« Reply #4 on: October 21, 2012, 06:42:02 pm »

That's thing thing... the root htaccess solution I showed above does force a www redirect to all pages and is the preferred method to do so.  Problem is it only works for the /snews/ base location.  Anything other than 'home' and it is not forced and I cannot understand why.

PS - sorry about the double post.  I was up till the sun came up at this desk and totally forgot I even posted the original thread.
Logged