htaccess rewrite secondary domain to subdomain - .htaccess

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]

Related

Domain name not redirecting

My website can be accessed via https://example.com but can not be found using either https://www.example.com or www.example.com, if I use either of these it just throws up a "site cannot be reached" page.
Is this something I can change in the .htaccess file or is it the DNS of the domain?
I have tried adding the following code to the .htaccess file, but it makes no difference
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
...or is it the dns of the domain?
Yes, this is "most probably" a DNS issue. The request is not even reaching your server by the sounds of it. .htaccess is consequently never processed.
You'll require a CNAME record that points the www subdomain to the domain apex (example.com) - so that both www.example.com and example.com point to the same place.
However, you also need to make sure that your server is accepting requests to both www.example.com and example.com - this doesn't happen by default. Although if you have configured your account via some hosting control panel then this has probably already been done for you. Otherwise, you need a ServerAlias record defined in the relevant <VirtualHost> container. For example:
ServerName example.com
ServerAlias www.example.com

Redirect subdomain into a folder of another domain

I'm trying to redirect one subdomain (a wordpress.com domain) into a folder of another domain. Example:
blog.domain.com to anotherdomain.com/blog
I know that is possible by using .htacces files, but i dont have a hosting service on my first domain.
Is it possible to do it by using DNS?
I have tried by creating some A and CNAME registrations but I cant find a way to do it: Can I have a ip for a specific folder of my second domain?
Thanks :)
I attempted to do a comment but it's too much. You can do this if you own both domains. If you don't have hosting on the first domain but you do on the other domain, then you can add the main domain as an ServerAlias on the other server.
Either through your control panel or your vhost config do the following.
Add an A record for blog.domain.com in DNS pointing to the IP address of the other domain with the hosting account.
In the configuration of the web server with the hosting add a ServerAlias of blog.domain.com to the anotherdomain.com vhost config.
in the root of the anotherdomain.com put this code inside your .htaccess file.
If you want it to actually redirect then you can do this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/blog/$1 [R=301,L]
If you don't want it to redirect and keep blog.domain.com in the address bar then, you can do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

.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]

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

Webfaction subdomain forwarding

I have shared hosting on webfaction and I want to have www.mydomain.com forward to mydomain.com, in the same way that www.stackoverflow.com redirects to stackoverflow.com. In the webfaction control panel I set up a CNAME record linking the www.mydomain.com subdomain to mydomain.com, but this doesn't seem to be working. Maybe what's messing it up is that mydomain.com is a virtual host and doesn't have an A record to an IP address?
Anyway, can someone help me figure out the right way to do this, either in the webfaction control panel or directly in the httpd.conf file? Thanks in advance.
The solution I use is to create a Static/CGI/PHP application (which I call redirect), and place an .htaccess file at its root. I point all the domains and subdomains that need redirecting to this application, and I then populate the .htaccess file with all the necessary redirection directives for my server, like so:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www.example1.com
RewriteRule ^(.*) http://example1.com/$1 [R=301,L]
It works well, and because it is instantaneous, it's easy to debug.

Resources