I am moving to a NEW url. The previous URL was an established blog so there are quite a few URLs that need to redirect so I COULD use 301 redirect in .htaccess but the issue is that we want the core URL to redirect to a certain page.
I have already attempted:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR] RewriteCond % .
{HTTP_HOST} ^www.olddomain.com [NC] RewriteRule ^(.*)$
https://newdomain.com/$1 [L,R=301,NC]
and
RewriteEngine on
RewriteRule ^/$ newdomain.com [R=301,L]
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ RewriteRule (.*)$
http:/www.newdomain.com/blog/$1 [R=301,L]
So the redirects should work like this:
http://olddomain.com/path/to/page
redirects to http://newdomain.com/path/to/page
EXCEPT http://olddomain.com/
redirects to http://olddomain.com/blog
What is the most efficient method of redirecting without having to hand code over 200 redirects? Nothing I have found is working.
Related
I'm having some issues with some .htaccess redirects only working in certain situations.
The code I have is
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And here are the results of my tests
https://www.example.com redirects to http://example.com
https://example.com does nothing
https://www.example.com/page-name redirects to http://example.com/page-name
https://example.com/page-name does nothing
This is a WordPress site and the .htaccess code sits above the WordPress code. I have also tried it below too and I get the same results
It looks like the site didnt have an SSL attatched to it so the
RewriteCond %{HTTPS} on
rule was not working correctly.
Because of this I went down this method to fix the redirects and make it work in all of the test cases that I mentioned in the question.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Use this in your .htaccess instead:
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [R,L]
I'm assuming that you want www forced to not show, since you did not include it in the URL. If that is the case, include this extra condition to force it to not show:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [R,L]
Make sure you clear your cache before testing this.
I am wanting to redirect all my pages to https. Currently it is doing this but some websites when doing test state that I have to many redirects. This is basically what I have set up within my .htaccess file (for Wordpress)"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
NOTE: I also have another redirect within this file for another reason, as I have changed my URLs and wanting them all to redirect using a 301:
RewriteBase /
RedirectMatch 301 /market/(.*)/ /product/$1
I'm basically wondering if I have done it all correctly or is there a simpler way so there is not so much redirects. I also notice within my Plesk panel there is an option for this. Any help would be great.
I suggest using thos code in your htaccess file to force HTTPS redirection (make sure to adjust URLs to your actual ones):
RewriteCond %{HTTP_HOST} !^domain\.local$ [NC]
RewriteCond %{HTTP_HOST} !^staging\.domain\.com$ [NC]
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
I'm trying to move a site from a set of static html pages on one domain, to a new domain on WordPress. So I want each old page redirect to the corresponding new page on the new domain.
The structure on old domain is like so:
http://www.example.com/page.html
and the new domain has this structure:
http://www.example-new.com/category/sub-category/page/
So I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page2.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page2/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page3.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page3/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page4.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page4/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page5.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page5/ [R=301,L]
And it works (kinda). The only GIANT problem is that now all pages redirect to the first new page, in this case http://www.domain-new.com/new-category/new-sub-category/page1/
Obviously I'm not an expert, but I spent hours on Google looking for an answer and trying things and can't figure how to do this, the closest I have found is about redirecting domain A to domain b removing the extension, so this is my last resource.
Any help appreciated!
This condition is the main problem:
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]
This will always evaluate to true because variable HTTP_HOST only matches host name part of a web request not the complete URL.
You can use this single rule in your .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-old\.com$ [NC]
RewriteRule ^([\w-]+)\.html$ http://www.domain-new.com/new-category/new-sub-category/$1/ [R=301,L,NC]
You are over-complicating this. Instead of all that processing, just use the Redirect Directive.
Redirect 301 /page1.html /new-category/new-sub-category/page1/
Redirect 301 /page2.html /new-category/new-sub-category/page2/
Redirect 301 /page3.html /new-category/new-sub-category/page3/
Redirect 301 /page4.html /new-category/new-sub-category/page4/
I tried with this
RewriteCond %{HTTP_HOST} subdomain.maindomain.com [NC]
RewriteRule ^(.*)$ http://subdomain.maindomain.com/wp-admin/$1 [L,R=301,NC]
but it's not working.
The behavior I want is for subdomain.maindomain.com too be automatically redirected to subdomain.maindomain.com/wp-admin
I'm trying to do a redirect from an old site to a new one:
OLD: blog.example.com
NEW: www.example.com/blog
To achieve the above, I got this .htaccess:
RewriteCond %{HTTP_HOST} ^blog.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^blog.example.com$
RewriteRule (.*)$ http://www.exmaple.com/blog/$1 [R=301,L]
RedirectMatch 301 ^/bournemouth-datacentre/$ http://www.example.com/blog/
And that works fine. On top of that I need to redirect (example):
OLD: blog.example.com/somecategory/some-title-here/
NEW: www.example.com/blog/some-title-here
Note that I'm loosing the category in between domain and title and replacing it with blog. That's fine.
Can anyone suggest how I can achieve both the subdomain to domain followed by blog redirect as well as the category redirects?
IMPORTANT: Old site is WP, new is Drupal!
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteRule ^(?:bournemouth-datacentre|general|london)/(.*)$ http://www.example.com/blog/$1 [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteRule ^(.*)$ http://www.exmaple.com/blog/$1 [R=301,L]
Try the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^blog.example.com [NC]
RewriteRule ^(.*)$ /bournemouth-datacentre/$1 [L,R=301,NC]