Overwriting 301 Redirect for specific pages - .htaccess

I am moving an old domain to a new domain however some of the old pages are not available. Therefore pages that are still available on the new domain need to be redirected to the new existing page and all others need to be redirected to the overall domain. I am having the following redirect rules:
Updated
//Specific
RedirectMatch 301 ^/(title_1/?)$ https://www.website.com/$1
RedirectMatch 301 ^/(title_2/?)$ https://www.website.com/$1
RedirectMatch 301 ^/(title_3/?)$ https://www.website.com/$1
...
//Overall
RedirectMatch 301 ^/(.*)$ https://www.website.com/blog
However the overall first rule overwrites all the other following rules which are made for the existing subpages. There are about 200 Subpages which are added to the htaccess. Also what I need to mention is that the overall website runs on domain.com/blog but all the subpages that are redirected are just domain.com/title_name_1.
I need the specified 200 pages to not be overwritten by the first rule. Thus not getting redirect to /blog.
I am kind of lost with this. Any help appreciated :)
Thanks

Change order of your rules and better to use same directive RedirectMatch for all redirects:
//Example of Redirect for specific pages (about 200+)
RedirectMatch 301 ^/(title_1|title_2|title_3)/?$ https://www.website.com/$1
RedirectMatch 301 ^/(category/title_name_2/?)$ https://www.website.com/$1
//Overall redirect for not specified pages in htaccess
RedirectMatch 301 ^ https://website.com/blog/
Make sure clear your browser cache or use a new browser for testing this change.

Related

htaccess Redirect 301 redirects to wrong url

I've created a bunch of 301 redirects in my .htaccess, for example
Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
and so on.
When I enter
www.old.com/site_1/ (wrong)
I get directed to
https://www.new.com/de/site_1/ (services folder missing)
It seems that the parent folder is missing in the redirected URL. Same for all other sites that reside in folders.
Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
Since the mod_alias Redirect directive is prefix-matching, a request for www.old.com/site_1/ would actually get caught by your first (most general) rule. And everything after the match (ie. site_1/) gets appended onto the end of the target URL (ie. /de/), so the resulting redirect becomes /de/site_1/ (but not to new.com as you've stated?).
You could resolve this by reversing the directives, to have the most specific matches first. For example:
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 / /de/
Or, as you mentioned in comments, use RedirectMatch instead - which is not prefix-matching and matches against a specific regex instead. Although you will still need to modify the pattern. Something like:
RedirectMatch 301 ^/$ /de/
RedirectMatch 301 ^/site_1/$ https://www.new.com/de/company/site_1/
RedirectMatch 301 ^/services/site_2/$ https://www.new.com/de/services/site_1/
Although this now matches the exact URL, which may or may not be what you require.

Redirect 301 for entire site with exceptions

I've been unable to find a reliable solution to this.
I want to redirect all of a domain to a new domain, i.e.
www.olddomain.com/post236 redirects to www.newdomain/posts236
But I have a couple of exceptions
i.e. www.olddomain.com/about-us/ redirects to www.newdomain.com/about/
How can I do this?
You can use the following Redirects in htaccess :
RedirectMatch 301 ^/about-us/?$ http://newdomain.com/about
RedirectMatch 301 ^/(.*)$ http://newdomain.com/$1
Note : The second line redirects all requests from old domain. If you have more exceptions on redirects ,just put them above this.

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.

How to permanently redirect to new domains startpage?

If I wan't to redirect the whole site (from all pages) http://myoldsite.com to http://mynewsite.com I add this to myoldsites .htaccess:
RewriteEngine On
RewriteRule ^(.*)$ http://mynewsite.com/ [R=301]
That works fine, every page now goes to http://mynewsite.com.
But there is a problem, http://myoldsite.com/apage will go to http://mynewsite.com/apage. I wan't all pages to redirect to mynewsites startpage (http://mynewsite.com).
How can I do this?
Why on earth are you trying to do a permanent redirect with a rewrite rule?
RedirectMatch 301 ^/ http://example.com
It's called a permanent redirect for a reason. The code 301 isn't for your average user, it's for search engines.
EDIT: Redirect 301 / http://domain.com redirects to a new location, but it also takes consideration for anything you've typed after the basic domain, for example a folder.
What you need to use is RedirectMatch 301 ^/ http://example.com which will redirect everything to http://example.com.

.htaccess 301 redirect page with no extension to a PHP page

I'm having a problem getting an old WordPress site page to redirect to the new basic PHP site page.
Example: The old WordPress page with no extension is at http://example.com/levelone/leveltwo/pagename
The new page is at http://example.com/directory/pagename.php.
Here are several things I've tried:
redirect 301 /levelone/leveltwo/pagename http://example.com/directory/pagename.php
This did not work at all
Then I tried redirecting the directories first, then the page, like so:
RedirectMatch 301 ^/levelone/leveltwo/ http://example.com/directory/
redirect 301 /pagename http://example.com/pagename.php
This almost worked, but gave me the right URL but without the PHP extension.
I can't just redirect an old directory to a new one because there are actually many. The example is just one. The trouble seems to be going from a non-extension page to a page with the .php extension.
Here's another thing I tried:
RedirectMatch 301 ^/levelone/leveltwo/(.*)$ /directory/$1.php
redirect 301 /pagename http://example.com/pagename.php
This gave me http://example.com/directory/pagename/.php.
Solved: I got it to work with the following:
Redirect 301 /levelone/leveltwo/pagename/ http://example.com/directory/pagename.php
The problem seemed to be with the missing forward-slash after the old page name.
Try:
RedirectMatch 301 ^/levelone/leveltwo/(.*)$ /directory/$1.php
What's probably happening is the mod_alias and mod_rewrite aren't playing nice with each other. They're both in the URI-file mapping pipeline so when one does its processing, the URI (eventhough a redirect response is what's going to ultimately happen) continues to get processed, then when the redirect happens, the URI has been mangled by mod_rewrite.
You should just stick with mod_rewrite so that you can prevent any wordpress rules from doing its thing. Add these rules above any wordpress rules you have in your htaccess file:
RewriteEngine On
RewriteRule ^/?levelone/leveltwo/(.*)$ /directory/$1.php [L,R=301]

Resources