Redirect to a .html if from some domain - apache

I have two domains that are pointing to the same directory, but I would like to redirect them (via mod_rewrite in htaccess) to some specific .html when hitting home.
Like:
if (domain == 'firstdomain')
redirect firstdomain.html
else if (domain == 'seconddomain')
redirect seconddomain.html

[..] I would like to redirect them to some specific .html when hitting home.
A language like PHP is better suited for redirecting a single page.
The following code will redirect http://example.com/ to http://example.com/example.html and http://example.org/ to http://example.org/anotherpage.html. http://www.example.com will be unaffected (note the www. part)
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^$ example.html [R]
RewriteCond %{HTTP_HOST} ^example.org$ [NC]
RewriteRule ^$ anotherpage.html [R]
The first line matches the HTTP Host field (a.k.a. 'domain') against example.com case-insensitive (^ marks the beginning of the string, $ marks the end).
If there is a match, the page will be redirected (second line, [R]) to example.html
The same story for the third and fourth lines.

Related

.htaccess 301 redirect whole URL including Domain

I need to redirect around 300 URLs on a multidomain site that has the same URL structure on the different domains. For example:
https://www.example.com/de/products.html needs to be redirected to https://www.example.org/de/products.html
So my usual approach does not work:
RedirectMatch 301 /de/products.html$ /de/products.html
I would need something like
RedirectMatch 301 https://www.example.com/de/products.html$ https://www.example.org/de/products.html
which obviously doesn't work or I just didn't get to work.
Not sure if important, but it's a TYPO3 instance.
The mod_alias RedirectMatch directive matches against the URL-path only. To match the hostname you'll need to use mod_rewrite with an additional condition (RewriteCond directive) that checks against the HTTP_HOST server variable (the value of the Host HTTP request header).
Also, since the URL structure is the same on both domains then you only need a single rule - just use the same URL-path from the initial request. No need to do one-by-one redirects as you seem to be trying to do.
For example, the following would need to go at the top of the .htaccess file before any existing rewrites:
RewriteEngine On
# Redirect everything from example.com to example.org and preserve the URL-path
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^ https://www.example.org%{REQUEST_URI} [R=301,L]
This checks for both example.com and www.example.com.
The REQUEST_URI server variable already contains a slash prefix, hence it is omitted in the substitution string.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
But I don't want to redirect all URLs, just some.
To redirect a specific URL to the same URL at the target domain, as per your original example:
# Redirect "/de/product.html" only
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^de/products\.html$ https://www.example.org/$0 [R=301,L]
The above redirects https://www.example.com/de/products.html only to https://www.example.org/de/products.html.
The $0 backreference contains the entire URL-path that the RewriteRule pattern matches against.
How to extend your snippet with /de/ or /fr/ etc.? For example I want to redirect example.com/de/products.html but not example.com/products.html
Maybe the above example is what you require. Alternatively, to redirect /de/<something> (or /fr/<something>) only and not just /<something>, you could do something like this:
# Redirect "/<lang>/<something>" only, where <lang> is "de" or "fr"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(de|fr)/[^/]+$ https://www.example.org/$0 [R=301,L]
The above will redirect https://example.com/de/<something> to https://www.example.org/de/<something>.

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

.htaccess - Redirect subdomain to main domain (only if NOT xyz.php)

Hi I would like to redirect the following sobdomain:
subdomain.domain.com to domain.com
ONLY if the address/script is NOT subdomain.domain.com/xyz.php
some examples:
subdomain.domain.com/index.php -> 301 redirect to domain.com/index.php
subdomain.domain.com/123.php -> 301 redirect to domain.com/123.php
subdomain.domain.com/xyz.php (no redirect)
I tried to redirect with the following code but I'm struggling with the condition. The redirect itself works... however for all pages:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.co.uk$ [NC]
RewriteRule !^subdomain.domain.co.uk/xyz.php$ domain.co.uk%{REQUEST_URI} [NE,R=301,L]
Finally I found a solution which works. if someone struggles with the same issue you can try this .htaccess code:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteRule !^xyz\.php($|/) http://www.domain.com%{REQUEST_URI} [NE,R=301,L]
The first line addresses the subdomain (case insensitive)
The second line redirects all sites Except the specified xyz.php to the main domain (301)

.htaccess 301 redirect all pages on old domain to a single page on new domain

I'm looking to redirect each and every page on the old domain to a single page on the new domain. It should also redirect both the www and non-www versions. Every single request to the domain old.com, whatever it may be, should lead to www.new.com root.
old.com/1.php
www.old.com/2.php
old.com/2.php
old.com/links/index.php
old.com/about-us.html
old.com/?string=1
should all redirect to:
www.new.com/
I'm looking for a .htaccess solution.
You can use RedirectMatch in the old.com vhost
RedirectMatch permanent .* http://www.new.com/
Redirect appends the trailing portion of the matched URI to the redirection URI but with RedirectMatch it's up to you to choose the parts (if any) that you want to transfer using parentheses and $number references.
If the old and new domains must be aliases for the same document root on disk then you'll probably have to use mod_rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/ [L,R=301]
This has already been answered, but here's the code altogether containing helpful comments so people have a reference to look back on later should they forget what it does.
Put this in your .htaccess file:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/? [L,R=301]
You can also redirect to a sub directory on another domain, as was the case that I needed to do like this:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/sub-directory/? [L,R=301]
This will handle all your redirects including query strings. Excuse the formatting, I'm on a phone :)
RewriteCond %{HTTP_HOST} old\.com$ RewriteCond %{QUERY_STRING} .+
RewriteRule (.*) http://www.new.com$1?%{QUERY_STRING} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule (.*) http://www.new.com$1 [L,R=301,QSA]

htacces rewrite tld without changing subdomain or dirs

I am trying to rewrite the following url:
the subdomain should match any subdomain. same for the TLD.
both: http://car.example.com/ and http://cat.example.co.uk should be rewritten
http://subdomain.example.com/some/dir
to
http://subdomain.example.nl/some/dir
and
http://example.com/some/dir
to
http://exampkle.nl/some/dir
(also with www. adress)
but my knowledge of htaccess and rewrite rules in general aren't good enough for this :(
I hope one of you knows the solution.
ps. I did try a search ;)
The challenge comes with having to detect and account for four different possible domain patterns:
example.com → example.nl
example.co.uk → example.nl
sub.example.com → sub.example.nl
sub.example.co.uk → sub.example.nl
So, what this ruleset does is checks that the TLD is not .nl (preventing a loop from occurring), then pulls the subdomain, www or not, off the front (read as "capture anything other than a dot followed by a dot, optional), followed by the base domain, followed by a dot. We don't have to match the entire URL, since we aren't keeping the TLD.
RewriteEngine On
RewriteCond %{HTTP_HOST} !example\.nl$
RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.
RewriteRule ^ http://%1example.nl%{REQUEST_URI} [NC,L,R=301]
The RewriteRule's ^ matches any URL, then inserts the contents of the first set of parens in the preceding RewriteCond (the subdomain) with %1, and completes the rewriting by appending the requested path and flags to ignore case, make this the last rule, and redirect with a search-engine-friendly 301, ensuring the rewritten URL appears in the user's browser. Any query string (text appearing after a ? in the URL) is automatically included by default.
Try this:
EDIT: See changes to subdomain, using %1 to capture from RewriteCond
RewriteEngine On
# Check if the hostname requested is subdomain.example.com or empty
# Now we attempt to capture the subdomain with (.*)? and reuse with %1
RewriteCond %{HTTP_HOST} ^(.*)?example.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
# Rewrite it as subdomain.example.nl and redirect the browser
RewriteRule ^(.*) http://%1example.nl$1 [L,R,NE,QSA]
# Note: With the above edit for %1, this part should no longer be necessary.
# Then do the same for example.com, with or without the www
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://www.example.nl$1 [L,R,NE,QSA]