301 re-direct OLD home-page to specfic page, all else to NEW homepage - apache

I'm moving my site to a new domain where the content from the HOMEPAGE on the OLD domain now lives on a specific page on the NEW domain. My question is- how do i re-direct the OLD homepage to this specific page on the new domain, and then send every other request from the old domain to the homepage of the NEW domain. I know thats kind of confusing- here's an example. Working with apache so will most likely do this in the htaccess file of the old domain.
I need
old.com -> new.com/specific-page
everything else -> new.com

The easiest way to do this is by using mod_rewrite on the old domain. Put the following directives in a .htaccess file in the www-root.
RewriteEngine on
RewriteRule ^$ http://new.com/specific-page [R,L]
RewriteRule ^ http://new.com [R,L]
The first rule will only match when the url-path is empty (that is, if you request http://old.com/). Any url that is not matched by the first rule will be matched by the second rule and redirected that way.
When both rules work as expected, change the R flag to R=301 (so that it says [R=301,L]). This makes the redirect permanent.

Related

.htaccess 301 redirect page specific + wildcard the rest

The site I'm working has moved to a new domain, with a completely different site structure and file naming.
I've set up a .htaccess file to re-direct important pages to the equivalent pages on the new domain:
RewriteEngine On
Redirect 301 /oldpage1/ http://www.newdomain.org/newpage1
Redirect 301 /folder/oldpage2/ http://www.newdomain.org/newpage2
Redirect 301 /oldpage3/ http://www.newdomain.org/folder/another-newpage
...
...and so on.
Now, I want to catch all other URLs under the old domain and direct them to the homepage of the new site. So something like:
Redirect 301 /*/ http://www.newdomain.org
But how is this really done?
Thank you for any help!
Upfront, RewriteEngine On is not necessary with Redirect directives, because RewriteEngine is part of mod_rewrite, and Redirect belongs to mod_alias. These are two different ways of achieving a similar goal.
Now, just to answer the question, you may use RedirectMatch for this
RedirectMatch / http://www.newdomain.org
This will redirect any path from the old domain to the home page of the new domain.
Please note, that the Redirect directive uses the "old" URL as a prefix
Then any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.
This means
Redirect /oldpage1/ http://www.newdomain.org/newpage1
will redirect all URLs, that start with /oldpage1/, not just oldpage1 alone.
And finally, never test with 301!

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

How do I redirect an entire site to a new domain using .htaccess?

I have a site that is moving to a new URL, an exact copy of the original with the same pages and URL structure. I'm working with the old domain's .htaccess file to redirect to the new one, and have this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule (.*)$ http://newsite.ca/$1 [R=301,L]
The redirect works when I go to oldsite.com or www.oldsite.com works as expected, and redirects to newsite.ca
However, when I go to something like oldsite.com/blog/, no redirect happens. It just stays at the same URL.
I want to be able to redirect every page to the new site, not just the root.
oldsite.com/blog/ should redirect to newsite.ca/blog/
I need this for the whole site without having to go through and redirect every single page individually. Is this possible?
Figured it out-
I moved my code to the top of the .htaccess file.
The code wasn't being executed because there was a www to non-www before it. Apparently the code is run in order, and if a redirect is found, it will not run another kind of redirect following it.

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