A simple (probably stupid) question: is it possible to show different pages for www and non-www versions of a website?

I mean for example, www.example.com will show the content in /mainfolder and domain.com will show the content in /secondfolder.

I don't want to redirect, like if the user goes to www.domain.com he will be redirected to domain.com/www - I don't want that, I just wanna know how to show 2 different index.html files, one for the www version of the website and one for the version without www.

Or if possible, how can I create the www subdomain in cPanel (as it won't let me) - any way to override this?


I tried this .htaccess edit and it worked partially:

RewriteCond %{HTTP_HOST} =www.site.com
RewriteRule ^(.*)$ http://site.com/folder/ [P]

It now shows the contents of site.com/folder when users access www.site.com however it doesn't load the CSS of the page - any way to fix this?


Yes, this is definitely possible. But please note that, by convention, www.domain.com is just an alias for domain.com. So, what you describe is not at all standard and violates most users' expectations. It also runs at least some risk of search engine penalties. So, in my opinion, it is not a good idea, but yes, it's possible.

Exactly how you do it depends on the server and/or scripting technology. For example, you could do it with Apache, which would look like this (in a .htaccess file):

RewriteCond %{HTTP_HOST} www\.
RewriteCond %{REQUEST_URI} !^/whatever/?
RewriteRule . /whatever

This would send all requests for www.domain.com to /whatever, but would not affect requests on domain.com. This is probably overly simplistic for what you want, but you haven't provided a lot of detail.

You can do the same thing in a scripting language like Python or PHP, but how you do it depends on what exactly you want to do.

Also, the reason you can't create a www subdomain in cPanel is that it creates it automatically.

(Edited: forgot to add the second RewriteCond to avoid a loop.)

  • well, if i use that htaccess redirect, if the user goes to www.site.com > the address in the browser will then change to www.site.com/whatever or site.com/whatever - i don't want that. what i want is if the user goes to www.site.com he will be shown a page and the address in the browser will still be www.site.com; and if the user goes to site.com he will be shown a different page and the address in the browse will still be site.com; also is there a way to bypass cpanel and create www as a subdomain? – user3288194 Feb 9 '14 at 12:55
  • @user3288194: The code that Ed posted is not an "external redirect", it is an "internal rewrite" - the URL in the address bar will not change in this instance. – MrWhite Feb 9 '14 at 13:13
  • already tried that code but for some reason it redirects to site.com (the url in the address bar changes) and it does not load the content from the /whatever folder... – user3288194 Feb 9 '14 at 15:35

Just found the solution:

RewriteCond %{HTTP_HOST} ^www\.site\.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) /folder/$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.