htaccess url redirect with subdomain and path - apache

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

Related

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 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/

RewriteRule Redirects paths not working

I moved my website to a new server with a new CMS so I had to make a lot of 301 Redirects. 'Normal' 301 redirects didn't recognize the url path of my old urls so I tried to make RewriteRules, this is what it looks like now:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^Category http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
The first RewriteRule works, but as soon as there is a second path in the old url (the second example) the redirect will point to the main cateagy and not the subcategory. So it's basically ignoring the url paths...
Try to invert your rules, or to add a $ at the end of the first one :
RewriteRule ^Category$ http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
Explanation : Category/Subcategory is also matching the first rule, and as you have use a L flag in the first one, Apache will just use this first rule and don't bother to look further.
For general purpose solution, quoting from apache rewrite guide:
Move Homedirs to Different Webserver Description:
Many webmasters have asked for a solution to the following situation:
They wanted to redirect just all homedirs on a webserver to another webserver. They usually need such things when establishing a newer webserver which will replace the old one over time.
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just
redirect all /~user/anypath URLs to http://example.com/~user/anypath.
RewriteEngine on
RewriteRule ^/~(.+) http://example.com/~$1 [R,L]
In your case URL structure has changed so ôkio's suggestion would work.

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.

Create Apache mod rewrite rule to process all request to subdirectory

I want to redirect all incoming requests to a subdirectory, but can't figure out how. I looked here but that did not work for me.
So, if someone visits www.example.com/test, I want it to redirect to: www.example.com/test/subdirectory, but the URL still needs to be www.example.com/test
I also need the parameters to work, for example www.example.com/test/index.php?action=home must redirect to www.example.com/test/subdirectory/index.php?action=home.
I hope someone can help me!
So in fact what you ask for is not a redirect.
A redirect means the web server will send a new url containing the subdirectory, the browser will then request this new url, and show it in the url bar of the browser.
What you want is to tranparently map an directory structure to a real directory structure which differs. This is what Apache can do easily with the Alias instruction. Mod-rewrite could also do that, with RewriteRules not containing the [R] tag, this would imply an internal rewrite, not a redirect HTTP response. A [QSA] tag would also tell mod-rewrite to keep the current query string parameters on the rewritten url. But Mod-rewrite is not always simple to understand, and your problem is simple enough for a basic Alias, which is almost always included in Apache (more often than mod-rewrite).
this should be ok:
Alias /test /test/subdirectory
Try adding the following to the .htaccess file in the root directory of your site.
Query string parameters are passed through by default.
RewriteEngine on
RewriteBase /
#if not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to subdirectory
RewriteRule ^(.*)(/[^/]+)?$ /$1/subdirectory/$2 [L,QSA]