Jump to content

Recommended Posts

Posted

I tried using the redirect in cpanel but got an error when trying to go to my site so I removed the redirect. I'd like someone typing example.com to get redirected to www.example.com. I've read about using .htaccess but don't have any files in my root directory... they all reside in my public_html/www directory.

 

I'm using phpNuke if that matters. :(

 

Thanks.

 

:P

Posted

I might not understand your question correctly so I better ask. :P

Are example.com and www.example.com the same site?

If so, anyone typing example.com will end up at your www.example.com by default. :(

Posted

Robert all your web site files are in your public_html folder

the www folder is a shortcut to the public_html folder

 

your .htaccess file is normally a hidden file and also in the public_html folder.

You can see it in cpanels file manager or an ftp program if you enable viewing hidden files.

 

If typing in domain.ext does not goto your web sites public folder

but www.domain.ext does

please open a help desk ticket to customer support and they get it working for you.

Posted

Thomas: Yes, it's the same site.

 

Thomas/Don: It's a nuke site with login. If I type "www.example.com" into my browser window, it logs me in. If I go to "example.com" only, it does not. It displays the site fine but I get the login window, and my address bar says "example.com". I've been to sites that if you type "example.com", it automatically changes (in the address bar) to "www.example.com"

 

Thanks :P

Posted

I may have done it wrong but last time I tried to do that the mysite redirected to www.mysite which is the same place so it tried to redirect and got very confused and spewed an error at me. Since both are the same directory and files I don't think it can happen. The other sites probably don't have a www symlink so they can do that. Hang tight and someone who can confirm or deny that which I said will be along and hopefully they will know a solution.

Posted

I'm trying to do the opposite of what you're doing--I want www.example.com to redirect to example.com. This code works for me:

 

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

 

I have it in an .htaccess file at my site root. Instead of going through CPanel's redirect option, open the File Manager, navigate to /public_html/, and if you don't already have an .htaccess file create one (use the create new file link, then just call it .htaccess with nothing in front of the .)

 

Then you'll need to reverse the above code, which I think would look like:

 

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

 

Open your new .htaccess file and paste the above in the file, and it should work for you.

 

A word of warning--if you already have a lot of redirects going on, this may not work. I know of at least one folder on my site that is having problems with this redirect clashing with another, and the server has decided to just send me back to the home page instead of to the page I expect.

 

But if you don't have any other redirects going on, then this should be ok.

Posted

thanks for the help everyone. :blink:

 

owatagal: tried your suggestion, and it didn't work. (I did change the "yoursite" info too :P )

 

it's not too big of a deal, just "one of those things" I'd like to fix. i'll try any other suggestions people may have.

 

thanks again!

Posted

eureka! it did work! what I had to do was change:

 

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

 

to this:

 

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^\example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

 

very slight change, just took the . out of the second line before "example" since there would be no period before that.

 

works great!!

 

:P :blink:

Posted

You could probably drop off that first backslash too, then, because it was escaping the period in the original bit of code.

 

Pesky periods... glad it worked, though!

Posted

Great job guys!

Way to stick with it Robert! Thanks to owatagal too! Now I have a new tool in my box as well. Thanks!

  • 4 weeks later...
Posted

I am having the same problem the first person is having. Some parts of my site require a password and the url needs to be www.example.com not example.com. I tried the rewrite rules but they did not work for me. Doesnt anyone elese have an idea?

Posted

Which version of the rewrites rules in this post did you try? And do you have other rewrite rules in your .htaccess that might interfere with this? And is the rewrite rule itself not working (i.e. the site doesn't direct to www.example.com) or does the rewrite work but the login doesn't?

Posted

I have tried both

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^\example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

and

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

I have no other rewrite rules in my .htaccess file. and yes the

rewrite rule itself [does]not working (i.e. the site doesn't direct to www.example.com)
Posted

I think the problem is in the backslashes--since the code is adapted from something where I was doing the reverse (stripping the www off the URL), the first backslash in the second line was there only to escape the period. See what happens if you take that backslash (and period) out:

 

>RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

 

I tried this on a site that doesn't have any other redirects, and it worked ok. I hope it fixes the problem for you.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...