.htaccess mod_rewrite order in Wordpress - wordpress

I'm trying to redirect my old webpage traffic to another while keeping some links working and pointing to same content on the new site using .htaccess RewriteRule in Wordpress.
I use pretty permalinks and while these are redirecting fine as soon as I try to redirect rest of the content they doesn't resolve.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog$ http://newsite.com/news/ [R=301,L]
RewriteRule ^news/(.*)$ http://newsite.com/news/$1/ [R=301,L]
RewriteRule ^old-category/(.*)$ http://newsite.com/news/$1/ [R=301,L]
</IfModule>
The above works fine and produce proper redirects even if someone uses link like
http://www.myoldsite/?p=1045
As soon as I add something like
RewriteRule ^(.*)$ http://www.newsite.com [R=301,L]
It produces links like this
htttp://mynewsite/?p=1045
I can't wrap my head around the order that these rules are processed. I want to let the pretty permalink generate and then if it doesn't follow the any of the above rules go to the home page under newsite.com. How do I do this?

Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=1045$
RewriteRule ^(.*) http://newsite.com/tutorials? [R=301,L]
RewriteRule ^blog$ http://newsite.com/news/ [R=301,L]
RewriteRule ^news/(.*)$ http://newsite.com/news/$1/ [R=301,L]
RewriteRule ^old-category/(.*)$ http://newsite.com/news/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.newsite.com/? [R=301,L]
</IfModule>

Related

RewriteCond %{REQUEST_URI} !=/ Is not working

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>

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

(.*) redirect loop Wordpress

Trying to proper make a redirect loop for site
tryng to redirect all sites like
34www.arsenal.org.pl / 46www.arsenal.org.pl / ww32w.arsenal.org.pl / dalx.arsenal.org.pl
to arsenal.org.pl
http://www.webconfs.com/htaccess-redirect-generator.php gave me the code
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://arsenal.org.pl/$1 [R=301,L]
but it give redirect loop in Wordpress
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)arsenal.org.pl$
RewriteCond %{HTTP_HOST} ^(.*?)\.arsenal.org.pl$
RewriteRule ^(.*)$ http://arsenal.org.pl/$1 [r=301,nc]
ty Cbroe for hint :)

Why am I getting a redirect loop with Wordpress htaccess file?

I'm trying to do a site-wide redirect from http to https for my Wordpress, and I'm trying to follow this tutorial - https://blog.nraboy.com/2014/12/force-https-pages-wordpress-site/
This seems to work for them, but when I follow it, I get a message saying the website is performing in a way that will never complete (or redirect).
My htaccess file currently looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can anyone please help me identify what's causing the redirect loop and how to fix it? I've put it into various testers, which seem to run through it fine. I have no plugins running, and can't figure out what's causing the loop.
I typically add the custom rewrite rules above the default WP ones.
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite non www. to www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite HTTP 2 HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# BEGIN WordPress
...
As you can see both rules redirect to HTTPS (the first one is applied only if non www. though).
Whenever a request is starting with https://www. the standard WP rulez (which you have posted and which are kept below in your .htaccess file) are applied as wanted.

Redirect loop when combining htaccess with permalink wordpress explanation

I have a problem redirecting links using htaccess rewrite. Originally I had only the Rewrite Rule part without the WordPress code underneath and it was working fine. Now, after I tried to add a permalink using Wordpress, the link is giving me a redirect loop problem.
Essentially what I want to do is allow the viewers to go straight to 'www.example.com/blog' when they type 'www.example.com'
I think I am missing something because I thought that the rewriteRule would stop the moment
RewriteRule ^$ /blog [R=301,L]
is processed because of the [L] flag and then other RewriteRules would not be looked at. Can someone help me understand how the server actually go through the steps of the code below?
RewriteEngine on
RewriteRule ^$ /blog [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
You need to put the line:
RewriteRule ^$ /blog [R=301,L]
In the htaccess file in your document root, not in the /blog directory.

Resources