Redirect any subdomain to same subdomain of different domain - .htaccess

I've moved my site from floriskleijne.nl to floriskleijne.com, and now want any visitor to any subdomain or subdirectory of .nl to be redirected to the .com equivalent of the URL. The challenge is in the subdomains: I have a number of those, and I want them to all be redirected to the equivalent subdomain on the new domain.
So for instance,
en2nl.floriskleijne.nl/ should redirect to en2nl.floriskleijne.com/
www.floriskleijne.nl/about/the-author/ should redirect to http://www.floriskleijne.nl/about/the-author/
floriskleijne.nl/about/the-author/ should also redirect to www.floriskleijne.nl/about/the-author/
I found the solution in this thread, but this does not seem to work for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.floriskleijne\.nl$
RewriteRule (.*) http://%1.floriskleijne.com/$1 [R=301,QSA,L]
There are two problems with this approach:
It doesn't work on my site: a subdomain call to the .nl domain gives me an "Apache is working normally" page at the URL I enter, with apparently no redirection at all. And a subdomain call with subdirectories and/or files gives me a 404 error.
Though this htaccess does the job of redirecting to .com, it seems to me that it ignores the no-subdomain version http://floriskleijne.nl. And indeed,iIf at all possible, I would want that to be redirected the same way (identical path). Oddly enough, floriskleijne.nl/whatever.subdir does get redirected properly.
I've checked the DNS, and I have an A record for both floriskleijne.nl and *.floriskleijne.nl, both pointing to the same IP.

Related

Redirect An Unknown Source Domain To Subfolder Using .htaccess

I am interested in offering my users a "white label" service, wherein they will set up a CNAME record that points to my server.
For example, I would like client.theirdomain.com to actually redirect to https://example.com/client/, but keep the URL still as client.theirdomain.com (and, further, if they click a link that is actually https://example.com/client/something.html, it would appear to the user as client.theirdomain.com/something.html.
I attempted to do this in my .htaccess with the following;
RewriteCond %{HTTP_HOST} ^client\.(.*)\.(.*)$ [NC]
RewriteRule ^$ /client [NC,L]
For posterity, I also have a valid subdomain created (client.example.com) that directs to the root of my website, as well as a CNAME record created on a client's server (which points client.theirdomain.com to client.example.com).
When I attempt to use the above settings, however, I receive an error that there were too many redirects.
Thanks!

301 Redirect Rules for a Blog Migration

I want to implement 3 redirect rules for a blog migration where each page will be shifted to a sub-folder structure. It currently sits at a sub-domain.
I can't really screw this one up and want to make sure I nail the correct generic rules for the 3 type of existing URLs:
Homepage
Current:
https://blog.lang.example.com
Goal:
https://www.example.com/lang-country/news/
Category
Current:
https://blog.lang.example.com/category/category-name
Goal:
https://www.example.com/lang-country/news/category/category-name
Post
Current:
https://blog.lang.example.com/yyy/mm/dd/article-name
Goal:
https://www.example.com/lang-country/news/yyy/mm/dd/article-name
Is this something you can help?
Unless you have other URLs that you don't want to be redirected then you can do something like what you require with a single redirect near the top of the .htaccess file in the subdomain's document root.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.([^.]+)\.example\.com [NC]
RewriteRule (.*) https://www.example.com/%1-country/news/$1 [R,L]
Where %1 is a backreference to the lang subdomain in the requested host.
However, you still have the problem of where country should come from in the target URL, since this is not present in the source URL. You will either need to default this to something or implement some kind of lookup based on the language. This would need server config access (to configure a RewriteMap) if you wanted to do this in .htaccess. Or, you implement this redirect entirely in your server-side script (eg. PHP).
Note that this is currently a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect once you have tested that everything is working OK (if a 301 is required). 301 redirects are cached hard by the browser so can make testing problematic.

Redirect all subdomains to subdomain at different domain

Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
If you have an Apache server running on example.net and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https:// to http://. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
EDIT
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org) is in /public_html/. I have a number of subdomains, e.g. subdomains.newdomain.org which is in /subdomains/public_html/. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/, /public_html/subdomains/ but that means each subdomain is also accessible at newdomain.org/subdomains/ which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301 - Redirect with a 301 Moved Permanently code. You can change the code if you don't need a permanent redirect, e.g. 302.
NE - No Encoding - Don't URI encode the new address, i.e. keep % as %, not %25
L - Last - Stop processing rules
Note that the .htaccess file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
any.subdomain.olddomain.com/any/address.html?any=query&you=like
is changed to:
any.subdomain.newdomain.org/any/address.html?any=query&you=like

TYPO3 multidomains for developing

I use a developer domain (domain.com.dev) for my local installation. For that I created three domain records for one domain:
domain.com -> redirect to http://www.domain.com
www.domain.com
domain.com.dev
But to get domain.com.dev working I have to deactivate the first two one. Will be there an other solution to do that with constants or pageTSconfig?
edited:
Using now htaccess-redirect for non www.
# Redirect non-www to www and ignore dev subdomain.
RewriteCond %{HTTP_HOST} !(www\.|dev\.).* [NC]
RewriteRule .* http://www.%{HTTP_HOST}/%{REQUEST_URI} [L]
Above I forgot another subdomain, so the domain records now as followed:
First tree:
www.domain.com
dev.domain.com
Second tree:
sub.domain.com
dev.sub.domain.com
But if I call now dev.domain.com the links are parsed as www.domain.com. Before I used baseURL. Than I read somwhere, I can't remember where, that baseURL is outdated.
But if I don't use baseURL this trick will not work. And with sub.domain.com it will not work anyway.
edited II
I' have now following two domain records for the domain sub.domain.com:
sub.domain.com
dev.sub.domain.com
If only one is activated, the internal links will be like href="home.html", if the second is activated they are href="http://sub.domain.com/home.html". But I'm currently on the domain dev.sub.domain.com.
Cause I have more domains in one installation I need these records. But how can I get rid of this prefixed URLs?
If TYPO3 prepends the domain to the links, baseURL will be useless...
In my experience it is not a good idea to structure it that way. It is much easier using dev.domain.com for your dev system and then add this to the hosts file to point to your local system.
The non-www to www redirect should also be made with a .htaccess, not within TYPO3 (performance). To solve the problem with your current structure you could try to remove the domain.com from the list and do then redirect as mentioned. Then the .com.dev should also work.
If I'm understanding correctly you need to setup a baseUrl based on the new domains.
This can be done as follows in TypoScript:
[globalString = ENV:HTTP_HOST=dev.domain.com]
config.baseUrl = http://dev.domain.com/
[globalString = ENV:HTTP_HOST=dev.sub.domain.com]
config.baseUrl = http://dev.sub.domain.com/
[end]
Next, if you use RealUrl or CoolUri, you will also need to create domain-records for these in the Typo3 backend.
Hope this helps :)

.htaccess redirect from subdirectory to another domains subdirectory accordingly

I am trying to make a redirect from my primary domain to an secondary domain, but only if the primary domain's request is to a sub directory.
The sub directory I want to redirect from is FTP, so if the user makes the following request:
http://www.site1.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
it would be transformed to
http://www.site2.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
but if the user makes a request that does not involve the FTP folder, the user will not be redirected. Like so:
http://www.site1.com or http://www.site1.com/somethingelse/
I am, however; a bit lost when it comes to making .htaccess files. What I have tried to do so far is:
# Redirect users
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^ftp(.*)$ http://site2.com/FTP/$1 [L,R=301]
</IfModule>
Any directions or samples would be great :)
No need to use the rewrite engine for simple redirects. I think you just want to use the Redirect directive:
Redirect /FTP http://www.site2.com/FTP
By default, this will result in a "temporary" redirect response (HTTP status 302). If you're sure the URL of the second site will never change, you can cause a "permanent" redirect response (HTTP status 301) by adding the permanent argument:
Redirect permanent /FTP http://www.site2.com/FTP
Also, note that the path of URLs is case-sensitive. If you want http://www.site1.com/ftp to also redirect, you will either need to add a rule with the lowercase path,
Redirect /ftp http://www.site2.com/FTP
or use mod_speling.

Resources