How to redirect domain to https and add default language - apache

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.

Related

Redirect with .htaccess to exact URL provided

So I want to redirect from old sites url to new one. Lets say example.com/en/some/stuff/foo/bar needs to be redirected to example.com/some/stuff.
Here is what I have at he moment:
Redirect 301 /en/some/stuff/foo/bar/ /some/stuff/
The problem is that I end up being redirected here example.com/some/stuff/foo/bar, but I need as I defined inside .htaccess example.com/some/stuff.
How to redirect properly to exact URL I have provided without anything extra.
You can use this redirect rule with regex in your site root .htaccess:
RewriteEngine On
RewriteRule ^en/(some/stuff)/.+ /$1 [L,NC,R=301]
Make sure to test it in a new browser to avoid old cache.

Redirect all pages from old domain to new specific URL without trailing slash content

I've got an old domain, let's say oldomain.com where I need to redirect all traffic to a specific URL, newdomain.com/path
While redirects from oldomain.com go perfectly, anything with content after the trailing slash will be copied over in the newdomain url structure causing 404's.
For example visiting: oldomain.com/somepage will result in newdomain.com/pathsomepage
What I'm looking for are some rewrite rules that will redirect any and all traffic from oldomain.com to newdomain.com/path without changing the specific "newdomain.com/path" URL.
I'm currently using the rules bellow which leads to the result above:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com
RewriteRule ^(.*) https://newdomain.com/path [P]
PS: the redirect is going to a Magento store.
You are trying to reverse-proxy in your directives (the P flag in the rewrite), but since you are describing a redirect... In the old virtualhost you just need to add a simple Redirect directive like this:
Redirect / https://newdomain.example.com/
This will Redirect all requests no matter how they are made to the new domain. Example GET /something will be redirected to https://newdomain.example.com/something
If you want the target to be a fixed destination like https://newdomain.example.com/path no matter what, use RedirectMatch instead:
RedirectMatch ^ https://newdomain.example.com/path

htaccess url redirect with subdomain and path

I've got two subdomains connected to two domains and a path and I need to redirect to a a new domain with the same subdomain without the path i.e.
www.subdomainone.olddomain.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomainone.olddomaintwo.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomaintwo.olddomain.com/path/article -> www.subdomainone.newdomain.com/article
www.subdomaintwo.olddomaintwo.com/path/article -> www.subdomaintwo.newdomain.com/article
I am not familiar with htaccess rules and I have tried using the testers online but cannot solve my problem
You can use this generic rule on olddomain's site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.olddomain\.com$ [NC]
RewriteRule ^(?:fr(?:-ca)?/)?(.*)$ http://www.%1.newdomain.com/$1 [L,NC,R=302]
Apache has url rewrites set of simple regex that let you do any king of such operation, all incomming traffic URL wil be rebuild before pointing to the right path (if is that what are you looking for).
Write storng rules that do not rebuild old URLs that stay the same.
A mor trustes solution (as you migrated to new urls) is to rewrite all old URLs but not prompt users to the page but rather sending to the user a redirect HTTP to the new url so you avoid the physical user using them and they will be forgotten in a short time.
example request:
http://subdomain.olddomain.com/path/article/pme
htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.olddomain\.com$
RewriteRule ^(?:path(?:-pathy)?/)?(.*)$ http://%1.newdomain.com/$1 [R=301,QSA,L]
This accounts for a hyphen in the path also for a dynamic subdomain.
new url: http://subdomain.newdomain.com/article/pme

Apache Redirect to static location

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/

http://domain to https://www in one redirect

I am using a single domain certificate (only on the www. subdomain) and want to have all users using this.
Currently i have the following in my htaccess file:
## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
But the problem is, that this seems to lead to two separate redirects:
http://domain.com -> https://domain.com -> https://www.domain.com
This means 3 RTTs for the user until he reaches the correct page. In addition only the second redirect seems to be permanent, the first one (http to https) seems to be temporary,
Is there any solution which directly sends requests of from http to https+www?
Many thanks in advance!
There's only one redirect. The target is https://www.%1. There is no redirect to http://www.%1 and no redirect to https://%1. Only https://www.%1.
No idea how you arrived at the conclusion that this rule caused 2 different redirects. Is this what your access logs are saying? Or the browser's network console?