Apache Redirect to static location - apache

I need to do a redirect on any url from one domain to a one static url on another domain
example:
http://myexample.com -> redirect to http://example.myexample.com
http://myexample.com/example -> redirect to http://example.myexample.com
I have seen a lot of example where the redirect carries over the /example to the redirect site.
I DO NOT want this...I need any urls from one sight regardless of parameters to redirect to:
http://example.myexample.com

This should be straightforward, see below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myexample.com$
RewriteRule ^ http://example.myexample.com/? [R,L]
This will redirect anything hitting myexample.com regardless of the path to http://example.myexample.com/

Related

Change domain redirect all URLs exactly except one URL that must be redirect to a different URL

Change domain from example.net to example.com.
I must to redirect all URLs exactly except one URL that must be redirected to different URL.
To redirect all URLs exactly (www and non-www) I use:
RewriteEngine On
RewriteCond %{HTTP_HOST} (w*)example\.net$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
Till here everything is perfect.
But I need one URL from old domain to be redirected to a different URL:
example.net/en to be redirected to example.com/abc instead of example.com/en.
I got the fix here: https://webmasters.stackexchange.com/questions/128426/redirect-all-urls-exactly-except-one-url-that-must-be-redirected-to-a-different/128427#128427
I do not understand why on #StackOverFlow I get negative vote for this question.
I hope to help other users this fix!

How to redirect domain to https and add default language

I'm setting up an apache server and want to redirect several type of domain to https and also add default language zh_tw in url if zh_tw or zh_cn are not exists in original url
for example:
www.something.com/news -> https://www.something.com/zh_tw/news
something.com/news -> https://www.something.com/zh_tw/news
http://something.com/news -> https://www.something.com/zh_tw/news
http://www.something.com/zh_cn/news -> https://www.something.com/zh_cn/news
I use htaccess tester To test my code but it seems failed beacuse using different host will cause redirect ? Anyone can help me with this issue?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule !^(zh_tw|zh_cn)/ zh_tw%{REQUEST_URI} [R=302,L]
Your htaccess file looks fine. If the url does not contain the language code and starts with http, then first the user will be redirected to https and then he will be redirected to a new url containing the language code. So after two redirects, the user will be redirected to the correct page.
Two redirects cannot be applied at the same time. So the first redirect rule will be applied first followed by the second rule.

redirect exact url to another url

There are a bazillion examples online of doing redirects via apache's htaccess, but I can't find any example of redirecting a full URL match to another full URL.
For instance, I have an existing website at
https://example.com
How do I redirect some specific URLs for that domain to a different one:
https://example.com/login --> https://my.example.com/login
https://example.com/register --> https://my.example.com/register
For every other every other path on example.com I need to remain untouched so my site still works fine (i.e. /blog shouldn't redirect somewhere else).
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(login|register)/?$ http://my.example.com/$1 [L,R]
This will redirect example.com/login or example.com/register to http://my.example.com/
You can alternatively accomplish this using the RedirectMatch directive (if the newurl is on a diffrent webserver) :
RedirectMatch ^/(login|register)/?$ http://my.example.com/$1

Apache Redirect URL with parameters

I'm trying to redirect all traffic from one domain to a specific page, with parameter, on a different domain. For example, all these URL's should redirect to the same URL:
http://domain_1.com (and all subdirectories)
eg. http://domain_1.com/sub/page.php etc.
should redirect to:
http://domain_2.com/newpage.php?name=parameter
This is what I have. How do I get the QUERY_STRING to be part of the redirect? (name=parameter does not change)
RewriteEngineOn
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{QUERY_STRING}% name=parameter
RedirectMatch / http://domain_2.com/newpage.php
Thank you for any help

Strange behavior of htaccess redirect

My goal is to redirect an entire domain to another. Every URL of the old domain shall redirect to the root URL of the new domain.
To achieve this I'am doing:
redirect 301 / http://www.google.de/
Problem is that when I test it on http://localhost/randomPath then it redirects to http://www.google.de/randomPath, but not to the root URL of google.de.
Does anyone know how to solve this?
Use mod_rewrite for finer control on rules like matching Host name etc.:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.google.de/? [L,R]
? in the end of target URI will strip off any existing query string.