Redirect domain based on regular expression - apache

I need to change domains when i have a certain path on the url request. More specifically i need to identify first if a certain path (/path) exists on the url request. If exists then show the website with the domain www.mynewdomain.com/path/. If /path does not exists on the url request then show www.myolddomain.com or whatever the request will be.

You can use RewriteRule to redirect the request to the new domain. You can mention the config in the virtual host.
RewriteEngine On
RewriteRule /path(.*) http://www.mynewdomain.com/$1 [L,R]
This configuration retains the URL part after the /path token [ denoted by (.*) ] and is rewritten to the redirected path using $1.

Related

redirect a page to an url with #

I would like to redirect an url (let's say https://www.example.com/de/page-a/) to an url having a parameter with a # (https://www.example.com/de/page-b/#filter:fields=6).
I don't find the right rule using RewriteRule or Redirect. It always redirects to https://www.example.com/de/page-b/, ignoring the last part.
Can someone help me on that?
Best regards
Redirect ^de/page\-a/$ exemple.com/de/page-b/#filter:fields=51? [L,R=301]
You are mixing up the directives. The mod_alias Redirect directive takes a simple root-relative URL prefix (starting with a slash) as the source URL argument. So the above will never match and nothing happens. There are no [L,R=301] flags with the Redirect directive, which is used by mod_rewrite RewriteRule.
For example:
Redirect 301 /de/page-a/ /de/page-b/#filter:fields=51
You do not need to specify an absolute URL as the target if you are redirecting to the same scheme + hostname.
When redirecting to a fragment identifier (everything after the #) you do need to be careful of redirect loops since the fragid is not passed back to the server. In this case you are OK since you are redirecting to a different URL-path, ie. page-a to page-b. But you could not redirect from page-a to page-a (same URL-path and query string) and simply change the fragid as it will create a redirect loop. For this you would need to use JavaScript.
NB: Test with 302 (temporary) redirect first to avoid caching issues.
If you want to implement this using mod_rewrite (ie. RewriteRule) - perhaps if you are already using mod_rewrite - then you could do the following instead:
RewriteEngine On
RewriteRule ^de/page-a/$ /de/page-b/#filter:fields=51 [NE,R=301,L]
Note that there is no slash prefix on the RewriteRule pattern.
The NE flag is required here in order to prevent the # being URL-encoded in the response and treated as part of the URL-path.

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.

Apache rewrite path to paramater

How would i convert:
https://www.example.com/ja to redirect to https://www.example.com/?lng=ja
I've tried the apache rewrite rule:
RewriteRule ^ja$ /?lng=ja&%{QUERY_STRING}
but I just get a 404 when accessing https://www.example.com/ja
I've also tried:
RewriteRule ^ja$ /?lng=ja
Which just loads the home page with /ja but doesn't seem to add on the url param
Try RewriteRule ^ja$ /?lng=ja [R,L]
By default, mod_rewrite maps a URL to a filesystem path, but it can also be used to redirect one URL to another URL.
Use of the [R] flag causes a HTTP redirect to be issued to the browser. If a fully-qualified URL is specified (that is, including http://servername/) then a redirect will be issued to that location. Otherwise, the current protocol, servername, and port number will be used to generate the URL sent with the redirect.
You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.

Htaccess internal redirect from domain to another url

Yet another .htaccess rewrite question... But can't find a way to make it work
My goal :
http://one.domain.com -> http://another.domain.com/specific/url
I'm not looking for a redirect, but a rewrite.
one.domain.com is a vhost alias on another.domain.com vhost.
No need for URL rest ($1...) it can be a perfect match (one-page site).
My best shot below makes a redirect, which I don't want.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^one\.domain\.com$
RewriteRule ^(.*)$ http://another.domain.com/specific/url [L]
This cannot be done using RewriteRule - from the Apache docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL.
You may want to use a workaround, e.g. reading the page http://another.domain.com/specific/url with Curl and then outputting the contents using echo() (if you're using PHP). Anyway it looks like a hard-to-maintain solution.

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