Fake subdomain for different users while still browsing same folder structure - .htaccess

I'd like to create fake subdomains for different users for more vanity, and to make the user (in this case a company) feel they are in a more isolated environment.
For the sake of maintainability, it's important for me that all users still browse the same files, to avoid having to update files for every single user that exists when updating the code.
My website has one public part at root, let's say www.example.com. I'd like to be able to fake the following kind of subdomains:
user1.example.com
The true URL would be www.example.com/member/?user=user1. I'd like for the folder structure to follow the same pattern. www.example.com/member/settings/?user=user1 would appear as user1.example.com/settings/ and so on.
I assume this would best be achieved with .htaccess, no?.
What is the proper .htaccess code for this?
Thank you!

based on the information you've provided here's a bit of .htaccess not tested but might do the job.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=(.*)$
RewriteRule ^(.*)$ http://%1.example.com/$2? [R=302,L]
Once you find it working fine, replace 302 to 301
Note: It's important to note that you'll need to add a subdomain wildcard

In the end, this was super easy to solve! It wasn't solved the way I first expected though, using .htaccess. I solved it with something called wildcard subdomain.
When you register a new subdomain, enter * in the domain prefix, such as *.example.com. A folder for the wildcard subdomain will be created on your server, such as _wildcard_.example.com. Whenever you access site1.example.com, fakesub.example.com etc, the browser of the visitor will read the files in the wildcard.example.com folder.
The beauty of it all is that if I create a certain subdomain that I want to use, for example forum.example.com, this real subdomain will have priority over the wildcard subdomain, and files will be fetched from the folder for this subdomain, as opposed to from the wildcard subdomain folder.
I use PHP and need to know the subdomain to fetch the appropriate database for the current user. To do this, I use the following code:
$subdomain = explode('.', $_SERVER['HTTP_HOST'])[0]
With a wildcard SSL cert I have all of these subdomains secure.

Related

Automatically change domain name in URLs

I'm creating a mirror for my website and I need to automatically change domain name in URLs on my new website, using some .htaccess command on my new site.
What I need is to change all links from www.old.com/any_link.html to www.new.com/any_link.html for all users on www.new.com without changing anything in database (having the same database for both sites). So that I get two independent websites www.old.com and www.new.com working at the same time.
I know about redirect 301 from old site to the new one, but I need redirecting INSIDE new website, without changing anything on the old one.
Is that possible at all?
try writing this on .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]

Prevent Subdomains being viewed/crawled

cPanel only allows me to create 'AddOn' domains. I have pointed all my TLDS to the server which saves them under 'public_html/main/sites' directories '/site1.com' , '/site2.com' etc. mainwebsite.com will be served under 'public_html' and all my client sites under 'public_html/main/sites'
It also creates subdomains 'username.mainsite.com' How can i prevent google from indexing those subdomains yet still index the TLDS. And stop users from being able to access the TLD from the subdomain too?
If i created a RewriteRule would google still index the TLD? Or is there a better way to go about this?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.site1\.com$ [NC]
RewriteRule ^(.*)$ http://www.site1.com/$1 [L,R=301]
If the 301-redirect works (for you), it will work for search engines, too.
See Google’s documentation:
301 redirects are particularly useful in the following circumstances:
[…]
People access your site through several different URLs. If, for example, your home page can be reached in multiple ways - for instance, http://example.com/home, http://home.example.com, or http://www.example.com - it's a good idea to pick one of those URLs as your preferred (canonical) destination, and use 301 redirects to send traffic from the other URLs to your preferred URL.

Changing website name using mod_rewrite and htaccess

I want to rename a folder on my site from http://mywebsite.com/myfolder/ to http://mywebsite.com/mynewfolder/. The urls for the old folder name are all index by Google and may other sites have linked to mine. What is the correct way to ensure that visitors coming to the site on the old folder name will now see the new folder name? Should I chane the name of the folder on my server and then use mod_rewrite to force the new url (folder name)
this seems to work, but is it correct: Redirect 301 /myfolder /mynewfolder
also for SEO would it be better to use: /my-folder-name/
RewriteEngine on
RewriteRule ^oldfolder/ /newfolder/ [R=301,NC]
It is widely acknowledged that hyphenating (-) your URLs makes a small impact on SEO as it separates any keywords in your URL rather than having them read as one long string. However saying that I'm pretty sure Google is clever enough to have a go at working this out for themselves. I don't suppose it would hurt and it makes it easier for your user to read at the very least.

How do I tell search engines not to index content via secondary domain names?

I have a website at a.com (for example). I also have a couple of other domain names which I am not using for anything: b.com and c.com. They currently forward to a.com. I have noticed that Google is indexing content from my site using b.com/stuff and c.com/stuff, not just a.com/stuff. What is the proper way to tell Google to only index content via a.com, not b.com and c.com?
It seems as if a 301 redirect via htaccess is the best solution, but I am not sure how to do that. There is only the one htaccess file (each domain does not have its own htaccess file).
b.com and c.com are not meant to be aliases of a.com, they are just other domain names I am reserving for possible future projects.
robots.txt is the way to tell spiders what to crawl and what to not crawl. If you put the following in the root of your site at /robots.txt:
User-agent: *
Disallow: /
A well-behaved spider will not search any part of your site. Most large sites have a robots.txt, like google
User-agent: *
Disallow: /search
Disallow: /groups
Disallow: /images
Disallow: /news
#and so on ...
You can simply create a redirect with a .htaccess file like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} \.b\.com$ [OR]
RewriteCond %{HTTP_HOST} \.c\.com$
RewriteRule ^(.*)$ http://a.com/$1 [R=301,L]
It pretty much depends of what you want to achieve. 301 will say that the content is moved permanently (and it is the proper way of transferring PR), is this what you want to achieve?
You want Google to behave? Than you may use robots.txt, but keep in mind there is a downside: this file is readable from outside and every time located in the same place, so you basically give away the location of directories and files that you may want to protect. So use robots.txt only if there is nothing worth protecting.
If there is something worth protecting than you should password protect the directory, this would be the proper way. Google will not index password protected directories.
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=93708
For the last method it depends if you want to use the httpd.conf file or .htaccess. The best way will be to use httpd.conf, even if .htaccess seems easier.
http://httpd.apache.org/docs/2.0/howto/auth.html
Have your server side code generate a canonical reference that point to the page to be considered "source". Example =
Reference:
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
- Update: this link-tag is currently also supported by Ask.com, Microsoft Live Search and Yahoo!.

Display content as root from subdomain

I have a site running on a subdomain of new.site.com and I want to be able to just put something in the .htaccess file that will display the subdomain's content as if it was in the root.
Where the site is: new.site.com
The url that I want to use: site.com
Basically what I want to do is just not move the files from the subdomain to the root of my server.
Try using a domain pointer.
EDIT:
Not sure why my answer was downed. Let me elaborate a little more.
I do this myself on my websites. In my hosting company's control panel, I have the option to create domain pointers. What this does is set a domain to use the website of another domain.
Actually found this that works incase anyone was wondering:
RewriteEngine on
RewriteBase /
RewriteRule ^$ /subdomain/ [L]

Resources