Contao: Multisite & Multiple Domain Names - .htaccess

I am working with Contao (TypoLight).
We have several sites on the same Contao Installation.
Which also means it's the same root folder.
Each site has its own entrypoint, atleast its supposed to have.
Now let's assume I have Site A (www.sitea.com) and Site B (www.siteb.com).
Site A needs to be accessible through www.sitea.com
Site B needs to be accessible through www.siteb.com
Now according to the manual each site configuration should have its own domain name entered in the entrypoint. This I have done but now www.siteb.com is redirecting to www.sitea.com.
Does anyone know why this behaviour is happening?
Or do I need to wait für any DNS updates?
Or do I need to specify any rewrite rules in .htaccess?
Cheers!

for each entry point in your site structure, configure the Domainname without "www":
for sitea.com: sitea.com
for siteb.com: siteb.com
create a htaccess (.htaccess file in your webroot) and configure the Host to redirect from www.site[a,b].com to http://site[a,b].com:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Contao is now able to route properly.

The answer may not be linked with htaccess or whatever. It probably has to do with vhosts.
Here's what you may have done, and if not, this should explain why your configuration doesn't work:
In the httpd.conf file, sometimes you have an include of a vhosts directory.
If not, here's what I do: I setup Apache so that it reads all the vhosts in a specific directory (it's almost at the end of the httpd.conf file, so that the default directives are applied before including vhosts):
# Include Virtualhosts directory:
NameVirtualhost *
Include /web/vhosts/
Then in the dir, I set up all my vhosts:
olivier#Tt /web/vhosts # find . | sort
./labyz.vhost.conf
./olivierpons.vhost.conf
./wipwip.vhost.conf
./wogwog.vhost.conf
olivier#Tt /web/vhosts #
Then for each vhost I precise their own directives. Example (redirects all to http://disneyland.fr/):
<VirtualHost *>
ServerAdmin webmaster#olivierpons.fr
DocumentRoot "/web/htdocs/olivierpons/prod"
ServerName olivierpons.fr
ServerAlias *.olivierpons.fr
ErrorLog "/web/logs/olivierpons.error.log"
CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/olivierpons.fr/access.%Y-%m-%d-%H_%M_%S.log 5M" combined
RewriteEngine On
RewriteRule (.*) http://disneyland.com$1 [QSA,R=301,L]
</VirtualHost>
Note: in your case, the vhost files sitea.com.vhost.conf and siteb.com.vhost.conf have the same DocumentRoot.
Have you done that this way?

Related

.htaccess specific url redirect to another domain url [duplicate]

After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible.
What I want:
I have multiple domains with different extensions, for example:
mydomain.be
mydomain.nl
Now what I want is that the mydomain.be is redirected to mydomain.nl. The solution for this I have found on the internet and shown below, with the need of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.be$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.be$
RewriteRule (.*)$ http://www.mydomain.nl/$1 [R=301,L]
With this code, when you type mydomain.be you will be redirect to mydomain.nl. But also the URL in the addressbar is changed to mydomain.nl. What I want is to keep the URL in the addressbar mydomain.be.
So, mydomain.be:
keep URL
show content of mydomain.nl
How To?
It is possible to get it done via mod_rewrite but make sure mod_proxy is enabled in your Apache's httpd.conf. Once that is done enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.be$ [NC]
RewriteRule ^ http://www.mydomain.nl%{REQUEST_URI} [L,NE,P]
Take note of flag P which is used for handling the proxy request.
Read more about flag: P in mod_rewrite
Another option without hassling with .htaccess would be to point both domains to the same document root or setting one domain as an alias for the other, depending on how you are able to configure your Apache. However, this has downsides:
If your content management system uses absolute URLs a user who clicks on mydomain.nl on a link will be directed to the mydomain.be domain (WordPress does this, as an example).
Search engines punish this behaviour by placing you further down on the search results. at least Google does, they have an interesting blog post about duplicate content. Not sure about competitors.
An example apache config could be:
<VirtualHost *:80>
ServerName mydomain.nl
ServerAlias mydomain.be
DocumentRoot /var/www/mydomain.nl/htdocs
</VirtualHost>

Relocating the DocumentRoot using .htaccess files

I am trying to get all http requests to go to a different local directory than where DocumentRoot points to:
The web server config:
<VirtualHost 1.2.3.4:80>
DocumentRoot /local/home/me/example.com
ServerName example.com
However, the entire (PHP) code for this site is not at DocumentRoot but rather in a subdir:
/local/home/me/example.com/code/
Currently, to access the site, I have to use URLs that include the /code/ subdir. I like to get rid of that so that http://example.com/ accesses /local/home/me/example.com/code/
I understand that the right way is to make DocumentRoot point to the subdir. My web hoster won't let me change this setting, though (in fact, that's how I had it before, when I hosted this site on my private web server where I could set the DocumentRoot just like that).
I now hope I can accomplish this with rewrites in .htaccess file(s). I just don't understand how.
My questions, in particular:
What are the rewrite rules for this?
Can I make a single .htaccess file for all paths, or do I have to have one per subdir (e.g. there are subdirs inside /code/ - do they each need an individual .htaccess file?
You can use this code in your /local/home/me/example.com/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!code/).*)$ code/$1 [L,NC]
(?!code/) is negative lookahead that will check if request already doesn't start with /code/ and will add this if condition is true.

RewriteRule to an absolute path?

I'm trying to make RewriteRule redirect to an absolute path on the server, outside of the current directory. This works when correctly implemented in PHP (i.e. no privilege issue), but with .htaccess, it doesn't:
RewriteEngine On
RewriteRule ^files/(.*)$ /var/www/files/html/$2
This is an example, though. Imagine this .htaccess file is at /var/www/downloads/html and redirects to the other location. Like I said, it's an example and doesn't have to make sense in this case.
This probably doesn't work because it redirects to /var/www/files/html/var/www/downloads/html, I think.
Question: How do I make RewriteRule point to a directory outside of the current one?
Update: The VirtualHost for this site is:
<VirtualHost *:80>
ServerName subdomain.domain.com
DocumentRoot /var/www/files/html
</VirtualHost>
You can change DocumentRoot to /var/www and place your .htaccess there. The rule could then look like
RewriteRule ^files/(.*)$ /downloads/html/$1
But then, everything below /var/www will be publicly accessible. If this is not an option (e.g. security concerns), you must stick to your PHP solution.

Redirect non www. URL to URL with www

To let people access my website whether they type the www. in front or not where do I place the .htaccess file to do this as I have tried it and it won't work.
I now know that I need to place the .htaccess on the non www. server and need to know how to access this to put it there.
You need to place this file in the root of the folder. Make sure the file is called .htaccess and does not have an extension at the end ie .htaccess.txt.
If the directory in which your website is held for example is
/var/www/mysite
then this is the directory where the file needs to be placed. Depending on how your web server is set up, apache may not allow the use of htacess files. To overcome this do the following.
cd /etc/apache2/sites-available
and open “default” up in your editor or choice, eg
sudo nano default
Default for AllowOverride is none, it should be All, so your overall “default” file should look like this:
NameVirtualHost *
ServerAdmin admin#site.com
DocumentRoot /var/www/
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
If you do not have ssh access, you may need to speak to your webhost.
Additional Comments
If the htaccess file is confirmed as working then you need to add the following code to the htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This would forward all traffic to http://site.com
Or for the other way around
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Both of these will obviously need you to replace example.com with your site address. This is one way to do this, or you could add a ServerAlias in your apache vhost file for that particular site.
This has nothing to do with htaccess. You need to make sure that when someone types www.example.com, it goes to the exact same place as example.com. This is a DNS issue. Find your domain registrar and set that up.
Then on your server, allow for both in your vhost config. You should see something like this:
ServerName example.com
ServerAlias www.example.com

.htaccess with dynamics subdomains

I tried to make a .htacces to simulate dynamics subdomains.
like his post :
.htaccess: mod-rewrite; subdomain
but it's impossible for me to get the expected result.
i have read several times the doc at : apache
i have no problem with : RewriteCond, RewriteRule, regular expression.
But i have problems with the subdomain.
i have this page : tododiversion.es
and for my example, I want this :
chr.tododiversion.es ==> tododiversion.es/chr/
i put .htaccess in the www folder: www/ .htaccess
and the folder "chr" is in : www/chr/
in the htacces i put :
RewriteEngine On
# host starts with something else
RewriteCond %{HTTP_HOST} ^([^\.]+)\.tododiversion\.es$ [NC]
# rewrite
RewriteRule ^(.*)$ /chr [L]
it doesn't work..
I know that urlrewritting is working because I made a simple test.
I know that RewriteCond is working because I made a simple test.
I was trying on my computer with localhost and 127.0.01 and it didn't work neither.
If someone could give me some advice it should be great!
Chris
The issue could be that apache is not properly configured to handle subdomains. Make sure that you have all subdomains set up to forward to this directory, or the .htaccess will never be executed.
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
DocumentRoot /var/www/domain.com
</VirtualHost>
Remember to restart Apache after updating your config files.
For a reference, see How to configure subdomains for Apache2 on Ubuntu?
You need to include $1 in the rewrite rule so that any URL specifics after the domain are passed on.
chr.tododiversion.es/page.html ==> tododiversion.es/chr/page.html
Based on the link you provided, the rewrite rule should be as follows:
# rewrite
RewriteRule ^(.*)$ /%1/$1 [L]

Resources