htaccess, redirect to specific URL - .htaccess

I created a new website for a club. It has a new Domain as well as different directory structure. The important pages are correctly redirected, but i want to add a catch all on the old domain to the root of the new domain. I already tried various things, currently (on the old domain and host):
RedirectMatch 301 ^/?$ https://newdomain.com/
but whatever I try, this
olddomain.com/sub1/page1
is always redirecting to
newdomain.com/sub1/page1
How do I make a rule, catching all pages and redirecting to a specific URL without attaching the path to the target?
Thanks!

The pattern ^/?$ only matches / the homepage of your olddomain i.e. http://olddomain.com/ . You need to repace your current pattern with ^/sub1/page1 so that your http://olddomain.com/sub1/page1 can redirect to http://newdomain.com .
Change
RedirectMatch 301 ^/?$ https://newdomain.com/
To
RedirectMatch 301 ^/sub1/page1$ https://newdomain.com/
If /page1 is a dynamic value you can replace it with .+ .

Related

htaccess redirect old subfolders to non subfolders

I have an old site with urls A and B
A. www.site.com/about
B. www.site.com/about/subpage
I need to redirect
A. www.site.com/about-us
B. www.site.com/not-a-sub-anymore
My htaccess contains
Redirect 301 /about /about-us
Redirect 301 /about/subpage /not-a-sub-anymore
The first redirect works fine, however, my second redirect seems to inherit the first rule resulting in redirecting me to www.site.com/about-us/not-a-sub-anymore which results in a 404 obviously. How can I work around this issue?
edit: I am using apache2.2.3 and I see in apache 2.4.8 they've added a RewriteRule IgnoreInherit which seems like something I'd be looking for. I have no control of my version of apache unfortunately.
I would suggest redirecting via RewriteRule to prevent additional rules from firing:
RewriteRule ^about/subpage$ /not-a-sub-anymore [R=301,L]
RewriteRule ^about$ /about-us [R=301,L]
However, if you insist on using Redirect 301, I'd try rearranging the order:
Redirect 301 /about/subpage /not-a-sub-anymore
Redirect 301 /about /about-us

Redirect Website Homepage Only

I have two different websites that are hosted on different servers. I would like to redirect the homepage of the old site (www.oldsite.com.au) to the homepage of the new site (newsite.com.au). However, I do not want any pages within the old site (www.oldsite.com.au/page) to redirect to the new one (newsite.com.au/page). So, www.oldsite.com.au must redirect to newsite.com.au, but any other pages or files within the old site must not redirect.
All the .htaccess ideas I've tried just redirect every file from the old site to the new one.
Try one of these:
RedirectMatch 301 ^/$ http://newsite.com.au/
or
RewriteEngine On
RewriteRule ^$ http://newsite.com.au/ [L,R=301]
If you don't want a 301 (permanent) redirect, remove the 301 from the RedirectMatch or the =301 from the square brackets in the rule's flags.

Redirect all but some to new URL?

I am moving site and I want to do a 301 redirect on all but some of the URLs,
Like this:
oldsite.com/* -> www.newsite.com
oldsite.com/specific/article/to/redirect -> www.newsite.com/fancy/blah
So there are a few things I want to redirect to specific pages but all the rest should just goto root, how can this be done in .htaccess?
Add the following to the .htaccess file in the root directory of your old site.
#place your specific redirects first
Redirect 301 /specific/article/to/redirect http://www.newsite.com/fancy/blah
RewriteEngine on
#then your general redirect all to new site last
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [L,R=301]
There's RedirectMatch if you've got only one URL that needs to be exempted.
RedirectMatch permanent !/specific/article/to/redirect http://www.newsite.com
for multiple URLs, you'd probably be better off with mod_rewrite and an external rewritemap that lists the urls to be exempted.

301 redirect any page to one specific page

IS there a way with just htaccess 301 redirect to redirect any page on a domain to a specific page on another domain.
eg. I was domain.com/index.html and domain.com/contact.html to both redirect to newsite.com/index.html
But I am wanting to do this without having to list each of the pages specifically.
can my 301 redirect be just something like
301 * http://newsite.com/index.html
or how should it be set up. Unfortunately I don't have access to mod rewrite so I cant use mod rewrite to make it work.
Had an issue similar to this using wordpress and trying to remove all .asp extensions from pages, this worked pasted at the top of my .htaccess file
## 301 Redirects
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]
Yes, that is possible -- instead of mod_rewrite you need to use mod_alias (which has more chances to be enabled).
This one will redirect everything to index.html on newsite.com/
RedirectMatch 301 ^/(.*)$ http://newsite.com/index.html
This one will redirect everything to the same path but on another domain: (e.g. oldsite.com/meow.php => newsite.com/meow.php)
RedirectMatch 301 ^/(.*)$ http://newsite.com/$1

301 redirect everything to new root?

I am trying to do a 301 redirect of everything from an old subdomain to a new.
I have a simple .htaccess
Redirect 301 / http://www.smartphonesoft.com/
However if I goto the old URL with a subdir, it tries to redirect to the new domain with a subdir and fails.
ie
http://forum.smartphonesoft.com/reminder/
goes to
http://www.smartphonesoft.com/reminder/
When I would like it to goto
http://www.smartphonesoft.com/
How can I have everything simply redirected to the new domain root?
With Redirect you define the base path (path prefix) that is to be redirected; every path beyond that is redirected while just replacing the base path with the new base path.
If you want to stick with mod_alias, you can use RedirectMatch and omit the match:
RedirectMatch 301 ^/ http://www.smartphonesoft.com/
Assuming your server has support for mod_rewrite, you can do this:
RewriteRule . http://www.smartphonesoft.com/ [R=301,L]
Alternatively, sticking to mod_alias, this should also work (but I haven't tried it):
RedirectMatch 301 .* http://www.smartphonesoft.com/

Resources