.htaccess file exact match and not exact match - wordpress

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.

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>

Force add 'https://' in my cspecific pages by htaccess

I have a website built with WordPress cms, i want to add https:// on my contact page by htaccess file of website. I have try different think but i an not able to do it. Here is the site link
Please share if have any idea!.
If you have https enabled for your domain, it would be a good idea to use it for the entire website, not just for one/some pages only.
To redirect all existing pages from http to HTTPS, you can use the following .htaccess code snippet:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R,L]
</IfModule>
In your .htaccess - add this logic.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} contact
RewriteRule ^(.*)$ https://alacartesites.com/contact/$1 [R,L]
Ref: http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file, it worked for me. I verified. Please use the below one.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(contact|contact/)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Wordpress - moving a site to another domain, 301 redirects

I moved a wordpress site to another domain. I want to preserve the google links however and help it re-index the new site.
I did a 301 redirect but it only works for the root director. When someone goes to http://oldsite.com/directory/post-name-here/ I want them to be forwarded to http://newsite.com/directory2/post-name-here/. Both websites are in subdirectories. Is there a way to do it with in PHP? Or would I need a mod-rewrite?
What finally worked for me was this:
My oldsite file structure is like so:
/public_html/
index.php
/development/
.htaccess
To recap, I wanted to forward everything coming into the development directory to the new website. This is the code of the .htaccess in the development directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.org$
RewriteRule (.*)$ http://newsite.com/newdirectory/$1 [R=301,L]
Thank you so much to Peter
This has always worked for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
You have to add this rule to .htaccess file see below.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
If you have only one link under the directory. You can add a index.php file to http://oldsite.com/directory/ with this code.
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
If you have many links under the directory, then use the following code
if($_SERVER["REQUEST_URI"]=="/directory/post-name-here/"){
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
}
Here, we check if the file name is the old link, then redirect to new domain.

How to use .htaccess to remove '?m=1' in the end of url?

I just moved my blog from Blogger to WordPress, and have a problem with the mobile URL.
WordpPress have a function to setup URL structure, so the URL for desktop is OK, but there is an additional ?m=1 in mobile version Blogger URL.
This is what I'm trying to do:
redirect http://www.example.com/2016/05/artical.html?m=1
to http://www.example.com/2016/05/artical.html
I tried this but it didn't work:
RewriteCond %{QUERY_STRING} ^(.*)(^|&)m=1(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
Hope this helps just after RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]

.htaccess mod_rewrite order in 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>

Resources