Two sites/Two domains in one server - apache

I'm starting to manage an Apache web server and I have very little experience. I have two websites with two different domains (a.com, b.com).
A.com files is on the server root folder (/htdocs) and I have a .htaccess file configured for that domain already.
B.com is on a subfolder inside the server root (/htdocs/b/).
Therefore, depending on the "incoming" request domain, I want to somehow change the folder on the server (using .htaccess maybe?).
I have been reading some material on this, such as this tutorial, Apache guide, this blog post, and even this StackOverflow question, but nothing seems to have worked so far.
Changing the .htaccess is the correct thing to do? Can I do a mod_rewrite without the user noticing that the folder has been changed (keep it as "b.com" and not "b.com/b")?
Thanks in advance!

You can this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# if host is b.com
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
# silently forward to folder /b/ if not already /b/
RewriteRule !^b(/|$) /b%{REQUEST_URI} [L,NC]
PS: Though it will be better to change your VistualHost config and set /htdocs/b/ as DocumentRoot for b.com site.

Related

"www" in URL is causing a 404

I'm having a very strange issue where having "www." in my URL is causing a 404. I temporarily edited my .htaccess file to password protect the domain for development. I have since restored access and now if I go to www.domain.com it returns a 404.
However, going to domain.com works. The host provider is hostgator, and per their .htaccess guide (http://support.hostgator.com/articles/-htaccess-guidance) I have reverted back to the default config with the same situation:
DirectoryIndex index.html index.shtml index.php default.html home.html
I have even gone as far as deleting .htaccess alltogether in hopes that it would restore but no such luck.
What needs to happen here to allow a website to be accessed via "www.domain.com" and "domain.com?"
Your .htaccess file looks good as it stands; there is nothing preventing it through www.domain.com. The most likely issue here is an IP propagation one. www.domain.com is technically a subdomain of domain.com. Therefor, they use different IP addresses. It's possible that HostGator has cached the .htaccess information incorrectly for one IP, and not the other.
First port of call, try pinging both domains. Open up Command Prompt, and type:
ping www.domain.com
ping domain.com
See if one of them times out.
Ultimately, give it a few days, and try again. Your .htaccess file should copy over automatically, and the problem should be resolved. It's also possible that HostGator has configured something incorrectly on their end.
If after a few days simply waiting doesn't fix the issue, take it up with HostGator, or forcibly redirect your users to the non-www site:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Hope this helps!

Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot

Say you've got an Apache2 virtual host setup, something like this:
/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org
Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.
Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?
I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.
The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.
<IfModule rewrite_module>
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>
Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}?), but secure sites are comparably few and better resolved by IP address than by hostname.

Redirect entire directory as my home directory with mod_rewrite

I've been looking for an answer to this for a while and I've tried many tricks myself, but to no avail, so I bring my questions to all you wise people.
I have a website all ready to go. The entire contents of this website is in the main "www" directory of the site. So now when I go to www.mywebsite.com/main, it goes to the www/main.php file. However, I want to move the entire contents of my website into a new folder inside the www directory, which I will call "ts". So now, my main.php file will be in www/ts/main.php
However, the problem I am having is I want all URIs to remain the same, so I want to tell the server that www.mywebsite.com/main is now found at www/ts/main.php instead of where it was before (www/), and that all subsequent content is also now found in www/ts instead of www/.
I imagine that I will need a .htaccess file in my www/ directory which tells the server that the absolute path of www.mywebsite.com is found at www/ts/, but I am not sure how to do that. I also have an .htaccess file inside my new www/ts/ directory already redirecting many URIs, but I imagine that I need to tell the server that all the files within the directory need to ignore the /ts/ URI, again, I am not sure how to do that.
If anyone is able to point me in the right direction, I would be much obliged. Thanks.
EDIT: I am on a shared server so I do not have access to any of the conf files, therefore I need a solution involving mod_rewrite instead. Thanks.
You just need to specify where is the DocumentRoot of your VirtualHost.
Check Apache configuration for your Virtualhost (it's maybe the default one, but you could create one for your named site). And check the DocumentRoot tag.
If it was something like:
DocumentRoot /foo/bar/www
Alter it to :
DocumentRoot /foo/bar/www/ts
And that's all.
Edit:
With the restriction of only accessing a .htaccess and if we assume you will limit this new folder redirection for only one DNS name (as if you make a subdir it's maybe to make some other subdirectories later) this should work:
RewriteEngine On
# limit this rule on the www.mywebsite.com DNS name requested
RewriteCond %{SERVER_NAME} ^www\.mywebsite\.com$
# limit infinite recursion, when filename is ok...
# no, in fact not # RewriteCond %{REQUEST_FILENAME} !-f
# well this one is better, no loop for mod_rewrite, even on 404
# the rule is applied only once
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# so here's the rule, map the requested file on the filesystem in ts subdir
RewriteRule ^(.*)$ /var/www/ts/$1 [L]

Redirect non-www to www not working

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code
RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]
restarted apache and no effect and then tried
<VirtualHost *:80>
ServerName megahotserved.com
Redirect permanent / http://www.megahotserved.com/
</VirtualHost>
in the httpd.conf file, when I restarted apache firefox came up with an error
The page isn't redirecting properly.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
What should I do ?
your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:
RewriteEngine On
RewriteOptions Inherit
Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.
Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.

Problems redirecting old domain to new with Apache and htaccess

My homepage is located at www.nazgulled.net and I bought a new domain which is www.ricardoamaral.net. Both these domains point to the same exact server.
I'm having two problems with the redirection, my current code is this:
RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]
For now I'm just testing but the idea is to replace [L] with [L,R=301] when I'm ready to move the whole thing, just so you know.
Anyway...
1) This is not working, when I try it, I can't access "nazgulled.net", it gives me a "server internal error" and I don't understand why... I don't understand why because if replace "ricardoamaral.net" by "google.com", the redirect works just fine :/
2) I have a few subdomains and I would like to redirect everything in those too. My first choice is to add different rewrite conditions/rules for each of the subdomains but that takes a lot of manual code and if the user types some subdomain that doesn't exist, they don't be redirect it and I also want that.
I think for your situation creating separate vhosts would be the ideal fit. What I do often is place domains that are being redirected on the same server but in a different folder to keep the www folder clean, for instance. My main site would be here:
/var/www/example.com/public
For all my redirects I would place them like so:
/var/www/redirects/example-2.com/public
Within each public folder of the redirected sites you would add this line to your .htaccess file
RedirectMatch permanent /.* http://example.com/
For 1, you can use something like this instead:
<VirtualHost *:80>
ServerAdmin webmaster#nazgulled.net
ServerName nazgulled.net
ServerAlias www.nazgulled.net
Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>
I use a similar configuration and it works perfectly, keeping query parameters, paths, etc.
Regarding your own rewrite rule, I want to note that you may lose your query params if you don't include QSA.
To diagnose internal server errors, just have a look at the log file provided by your ErrorLog directive within your vhost, or a global one, depending on your configuration. It should give you the exact reason for the error.
This is best solutions. Create a notepad file and save it as .htaccess if you do not already have an existing one.
Update the .htaccess file with the following code and save
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]