.htaccess with dynamics subdomains - .htaccess

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]

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>

htaccess rewrite secondary domain to subdomain

I have a main domain "www.main.com" with subdomain "subdomain.main.com"
the subdomain is located in the folder /htdocs/subdomain/ and the main domain is a symbolic link to the /htdocs/www/ (which also serves www.main.com)
Now I have registered a new domain name, "newname.com" which I have connected to my hosting account. Now I am trying to determine the contents of /htdocs/.htaccess for rewriting incoming requests to subdomain.main.com.
To be clear, I do not want to redirect. I want www.newname.com/anything to be rewritten to subdomain.main.com/anything, while the user keeps on navigating newname.com.
I fear there is a catch with circular rewrites?
What should my .htaccess look like?
My document root is /htdocs and the subdomain and the new domain are on the same ip address.
First of all, make sure your DNS is properly setup i.e. ping www.newname.com goes to the same server (= same IP) than ping subdomain.main.com.
Then your vhost should look like:
<VirtualHost *>
ServerAdmin webmaster#papdevis.fr
DocumentRoot "/home/subdomain"
ServerName subdomain.main.com
ServerAlias www.newname.com
</VirtualHost>
Then everything should work transparently. It's all about vhosts. I don't know with htaccess, sorry.
It was in fact easy, i created a symbolic link to my new domain (primary.com/secondary) which links to the folder which also serves secondary.primary.com and I rewrite all queries to www.secondary.com to that symbolic link. Totally untransparent, just how I wanted it. It was just a bit fighting with drupal before it let me take control of a tiny part of my main domain :)
The main .htaccess is
RewriteCond %{HTTP_HOST} ^www.secondary.com [NC]
RewriteCond $1 !^secondary/
RewriteRule ^(.*)$ /secondary/$1
and i redirect all secondary.com to www.secondary.com in my rewrite base with this .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} secondary.com$ [NC]
RewriteRule ^(.*)$ http://www.secondary.com/$1 [R=301,L]

Contao: Multisite & Multiple Domain Names

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?

Is there a way to create subdomain like url redirection using .htaccess?

I want to write a .htaccess from which the following action should be done.
I have domain like
www.xyz.com
and am putting many articles on that.
so it wil become
www.xyz.com/article1-tutorial/
www.xyz.com/article2-tutorial/
www.xyz.com/article3-tutorial/
But instead of that i need like this.
www.article1-tutorial.xyz.com/
www.article2-tutorial.xyz.com/
www.article3-tutorial.xyz.com/
Please help to find the solution. I know we cant go for subdomain concept and only the way is redirection. So whats is the solution?
You'll need something like this in your apache.conf:
<VirtualHost *:80 >
ServerName automated_domains
ServerAlias *.xyz.com
VirtualDocumentRoot /home/xyzcom/website/
</VirtualHost>
Then in the PHP file you put in /home/xyzcom/website, you can get the domain that was actually called in the $_SERVER['HTTP_HOST'] variable.
Hope this helps!
KKovacs
You can use apache's mod rewrite like so:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [L]
when a user goes to http://article1-tutorial.domain.com/
server internaly rewrites therequest to http://www.domain.com/article1-tutorial
Can you use the reverse proxy engine of apache?
Reverse Proxying with Apache

Redirect 301 www.www.example.com

I'm using LAMP server, and I need to redirect requests like:
www.www.example.com to www.example.com and other variations like (wwww.example.com, etc)
I know I can do it in .htaccess, but I don't know the regular expression that I should use to represent all these possibilities.
Or there is any diferent approach, comming from the vhosts?
I like:
# force www IN URL
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# END force www IN URL
Basically anything that's not www.example.com will get redirect 301'd.
See this article for implementing "wildcard subdomains." You're going to need to implement both an .htaccess and modifying the Vhosts.
http://www.easymodrewrite.com/example-subdomains
If you're interested in only limiting it to "www.www," "wwww.," and the other examples above, you can easily do so with some server-side coding (which makes it more flexible than implementing more .htaccess code). Just detect what the subdomain is, and redirect where you want.
Using name based virtual hosts in Apache you can do this:
<VirtualHost *:80>
ServerName example.com
ServerAlias wwww.example.com www.www.example.com [space seperated list]
Redirect / http://www.example.com/
</VirtualHost>
Redirect can take a parameter to specify the type of redirect.

Resources