RewriteCond %{REQUEST_URI} !=/ Is not working - wordpress

I'm trying to redirect and entire wordpress site to a new domain EXCEPT one blog post, and the admin pages.
I've searched this site multiple times for various fixes, and across google searches. nothing has worked yet.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog-post-I-want-to-stay-on-old-domain/
RewriteCond %{REQUEST_URI} !=/wp-login.php
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>
I expected to be redirected with a full URL to a new domain, so oldsite.com/blog-post-1 would redirect to newsite.com/blog-post-1.
This works.
I expected the /wp-admin/ and the wp-login.php sections of the site to stay active without a redirect so I can continue editing the site if needed.
This works.
I expected oldsite.com/blog-post-I-want-to-stay-on-old-domain/ to not redirect, and be ignored, according to the ReWriteCond.
This does not work.
Instead it redirects to newsite.com/index.php and then newsite.com/
What have I done wrong?

Try this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !wp-login|wp-admin|blog-post-I-want-to-stay-on-old-domain
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>

Related

How to redirect all my website posts/pages from Http (+ www or non-www) to Https (non-www) URL

I recently updated my website from http to HTTPS & I don't have any prior knowledge about this. I did it following some articles on the internet & it was successful. My site is accessible through https & it's all fine.
But I'm stuck with redirecting the existing pages and posts. I'm not sure if I messed it up or I just don't know how to redirect properly.
Here's what I'm trying to do, I want to redirect all the URLs in the below format to redirect
from:
http://example.com/*
http://www.example.com/*
https://www.example.com/*
to
https://example.com/*
I've tried using the following code, but it's not helping.
# Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>
Any help would be appreciated. Thanks a lot in advance.
Found the answer after carefully doing some troubleshooting. The issue is with ReallySimple SSL plugin I used while updating to https.
By default, the plugin is redirecting all the traffic to https://www.example.com format which is showing an error page.
So I made the changes as per my requirement.
The problem is I didn't notice that this code exists & I'm looking at a wrong htaccess file in root folder instead of the htaccess file in public_html file.
# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.1]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L]
</IfModule>
I hope this is helpful if someone faces similar issue.

Exlude a page from htaccess Rewrite rule

I've a WordPress install where I have redirected all pages (except the wp-admin and wp-login) to a different url.
I want to exclude one page (called /your-login) from being redirected. This page is in effect the wp-admin page where the login slug has been changed.
How can I exclude this page from being redirected in the htaccess file?
The current code I'm using to perform the redirects in the htaccess file is:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(/wp-admin|.*wp-login\.php.*) [NC]
RewriteRule (.*) https://mywebsite.co.uk/$1 [R=301,L]
where mywebsite.co.uk would be the site being redirected to.
Thanks!
You can try this,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(/wp-admin|.*wp-login\.php.*) [NC]
RewriteCond %{REQUEST_URI} !^/your-login/$
RewriteRule (.*) https://mywebsite.co.uk/$1 [R=301,L]
Another solution is a plugin
https://wordpress.org/plugins/redirection/

Redirection with HTACCESS including GET parameters

I have a specific .htaccess defined as is, in order to reroute all traffic from root folder to identical URL in a subfolder with exceptions on sub-domains.
Here's my file:
#RewriteEngine on
#RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !^joe(.*)
RewriteCond %{REQUEST_URI} !^lab(.*)
RedirectMatch ^/$ http://www.mydomain.fr/blog/
</IfModule>
This file does exactly what I want: when I input something like www.mydomain.fr/wp-admin for example, it will redirect to www.mydomain.fr/blog/wp-admin, but will exclude subdomains joe.mydomain.fr and lab.mydomain.fr from redirection.
However, what happens is that if I go to joe.mydomain.fr?test=1, the server will simply redirect me to joe.mydomain.fr but the GET parameter disappears.
How can I avoid that?
I tried replacing [NC] with [QSA] or [R=301,QSA], but I get an Internal Server Error.
Thanks.
After spending my day documenting about how .htaccess files actually work, I was finally able to solve my problem with the following file, inspired by the one from Abhishek Gurjar:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /s
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^blog\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !/joe
RewriteCond %{REQUEST_URI} !/lab
RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301]
</IfModule>
FYI, the 3 first lines are mandatory with my provider 1&1 apparently...
What I changed is the REQUEST_URI from being !^joe(.* ) and !^lab(.*) to !/joe and !/lab and then excluding the blog.mydomain.fr URL as well to avoid problems with blog.mydomain.fr/joe or blog.mydomain.fr/lab if ever one day I use them.
Finally, I had to redirect my sub-domains in my 1&1 client interface to the subfolders of my webspace instead of the URL in hard, meaning:
Instead of redirecting joe.mydomain.fr to http://www.mydomain.fr/joe, I redirect it to /www/joe/
That configuration now allows me to type in an URL such as joe.mydomain.fr?dnt=1 while getting redirected to http://www.mydomain.fr/joe?dnt=1 which is exactly what I wanted to do.
I hope this helps others.
Thanks for the help.

How to 301 redirect all other URLs that do not meet conditions using htaccess

I am doing bunch of 301 redirects via the htaccess file for a WordPress website. I have listed about 80 specific URLs/directories that point to their new relative position on a new domain.
However, there are still a bunch of URLs that are lurking around, that I want to redirect to the new domains home page.
What I am trying to do is if the URL does not meet any of the hardcoded URLs, redirect it to the home page.
Here is what I have:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Begin Redirects
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/14/14/someother\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]
# End Redirects
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The code that I am using to catch all other URLs and send to the home page is written as:
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]
The issue I am having is that it seems to jump straight to that condition, redirecting all the URLs to the home page, whether that piece of code is at the top of the htaccess file or the bottom. Is there a way to set this as a condition so it only redirects the URL if it does not meet any of the URLs or directories above it?
If I remember correctly, RewriteRule can't be used like that, which normally we'll write it like:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteCond %{REQUEST_URI} ^/2015/12/14/some-directory/
RewriteRule (.*) https://www.newdomain.com/? [L,R=301]
Where your code is:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]
Also, you could test your .htaccess with this tool first which can save your time from transferring file between server and clearing cache (if you need).
Second, I don't really see the meaning of your code, since all URLs from www.olddomain.com will be redirected to https://www.newdomain.com/?. Unless you meant, redirect base on the path but not the domain, which you'll need to remove the first line of my code.
Last, notice I didn't add $ behind the path, since I'm not really sure if you need a wildcard redirect which /2015/12/14/some-directory/something also redirects to https://www.newdomain.com/?. If you don't, remember to add it back.

.htaccess file exact match and not exact match

I just moved to a WordPress site. I used to have a static site and a WordPress blog on a subdomain (blog.example.com). I'm using the code below, which is redirecting all of my blog posts to my new website perfectly fine. Is there any way to make an exception for the main blog page? To be specific, I need the code below to apply to all my posts, categories, etc. (which it already is), but I need my old blog home page (blog.example.com) to 301 redirect to my new blog homepage (example.com/blog). Just to be very clear, my posts, etc. are NOT using blog in the permalink.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>
UPDATE: Thanks, Olaf! Got this working with your advice. Here is my final code for anyone else looking for an answer.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^$ http://www.example.com/blog [R=301,L]
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>
Put the most specific rules at the beginning. In your case, prepend the existing rule with the redirect for the main page
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^$ http://www.example.com/blog [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.

Resources