descobrir

Registered
Jun 5, 2013
2
0
1
cPanel Access Level
Website Owner
I'm facing a problem with the redirect.

I used the code below and also redirect 301 of the cpanel with wildcard on. It worked but with a problem:

When the url is 'example.com/anypage.html' I want it to redirect for the same page but with the www. prefix ('www.example.com/anypage'). What is happening is that it redirects always to the homepage of the site ('www.example.com/index.html') instead of the specific page it should have.

What am I missing?
Thanks.

Code used in .htaccess
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,201
363
Hello :)

It looks like you essentially want to force the use of the "www" prefix. You could try using an entry such as the following in the .htaccess file:
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
Thank you.
 

descobrir

Registered
Jun 5, 2013
2
0
1
cPanel Access Level
Website Owner
Hi!

I used your entry but the behaviour remains the same.
If I remove the www. from the url it redirects always to 'http://www.example.com/index.php'


Thank you for the help.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,201
363
Are you sure there are no redirects within the script itself that is causing this behavior? Sometimes PHP applications use SEO rules or internal redirects that can conflict with existing redirect rules. Otherwise, you may want to try the Mod_Rewrite forums as there is typically more experienced users with custom Mod_Rewrite rules there:

Mod_Rewrite Forums

Thank you.