Welcome Guest, Not a member yet? Register   Sign In
www. redirect
#1

[eluser]mikeyhell[/eluser]
I'm trying to redirect any request to http://domain.com and https://domain.com to http://www.domain.com and https://www.domain.com. Here is the .htacces i'm having to use because it's got cpanel and the normal .htaccess i would use didn't work. Can I do this w/ .htaccess or would it be better/possible to do this in httpd.conf?

Code:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

i've also tried the following but get CI error message that my URI submitted has disallowed characters

Code:
rewritecond %{http_host} ^cartoonsinteaching.com [nc]
rewriterule ^(.*)$ http://www.cartoonsinteaching.com/$1

and also this is my config for the url's
Code:
$config['base_url'] = "http://".$_SERVER['SERVER_NAME'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

//$config['secure_base_url']    = "http://localhost/local_folder/";
$config['secure_base_url'] = "https://".$_SERVER['SERVER_NAME'];
$config['secure_base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

when I change "http:// to "http://www. or https://www. I end up with a url like this: http://www.www.domain so I'm confused as to what I need to do.
#2

[eluser]mikeyhell[/eluser]
easy fix w/ a str_replace("www.","",$_SERVER['SERVER_NAME']) in case anyone has the same thing come up.
#3

[eluser]Daniel Moore[/eluser]
You'll want something like this in your .htaccess file to do it from your .htaccess file. The following will ensure that search engines will only look to index your www prefix.

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

See my CodeIgniter .htaccess tutorial for more details on this.
#4

[eluser]mitchwilson[/eluser]
Thanks. Worked like a charm in .htaccess.

I needed this redirect to make sure sessions worked correctly when users left off www when logging in.




Theme © iAndrew 2016 - Forum software by © MyBB