Enable https using Rewrite Rule for specific URLs - wordpress

I am new to Apache and I am finding it extremely difficult to understand the rewrite rule syntax.
Here is my setup:
I have a Wordpress MU site at mydomain.com
I have parked domains site1, site2.com that point to mydomain.com
What I want:
I want this URL http://mydomain.com/wp-login.php to redirect to the same address but with https first. I have seen several tutorials on how to do this but there is a problem.
My problem:
If I use something like
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
this also redirects my other sites to https versions because they are parked domains and effectively loading the same htaccess code; since the certificate I have is only for mydomain.com I get certificate errors.
I need to only secure pages on the mydomain.com but only the wp-login.php page. Another issue is that this page can move around in the URL, IE:
mydomain.com/wp-login.php
mydomain.com/somecracp/wp-login.php
mydomain.com/wp-login.php?xys=5653
I need a regex which will detect the domain name of mydomain.com and the wp-login.php anywhere in the URL. I then need to force https for them. Can anyone build me the code please?
UPDATE:
I may have found part of the issue. It would seem that the login page itself is also trying to do some redirection. When I access the admin section of the site http://mysite.com/wp-admin it detects that you aren't logged in; it then redirects you to the login page but appends a redirect_to query string at the end like so:
http://mydomain.com/subdomain/wp-login.php?redirect_to=http%3A%2F%2Fmydomain.com%2Fsubdomain%2Fwp-admin%2F&reauth=1
It seems to do this so that once you log in successfully it can redirect you to the admin section you previously tried to access. This "redirect_to" and what comes after it is the URL it will redirect to once you log in.
I won't to go into details as to why but is there a way I can force it overwrite the above URL to go to mydomain.com/wp-login.php with none of the rediret_to crap?

Try this:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule wp-login.php https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
The regex part of the RewriteRule checks to see if there is any instance of wp-login.php in the request, and if there is, redirect to https.

Try
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} wp-login.php [NC]
RewriteRule https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The rewriteCondition will check if https is off and request uri contains wp-login.php, then redirect.

Related

htaccess redirect with exceptions

I work on a Wordpress Network. Let's say that the main site has the link www.main-site-old.com, and all the other sites in this network have links like www.main-site-old.com/site1 , www.main-site-old.com/site2 e.t.c.
I want to redirect ONLY the main site to a new domain (let's say www.main-site-new.com) and let all the others stay as they are.
I tried this one:
Redirect 301 / http://www.main-site-new.com/
but it redirects the whole domain (for example it redirects www.main-site-old.com/site1 to www.main-site-new.com/site1)
I also tried:
RewriteCond %{REQUEST_URI} !=/site1
RewriteRule ^.*$ http://main-site-new.com/$0 [R=301,L]
but nothing happened.
With mod_alias redirect you will not be able to use regex to capture a request for http://www.main-site-old.com only so, you could use another one RedirectMatch like this :
RedirectMatch 301 ^/?$ http://www.main-site-new.com
Or go to mod_rewrite by this :
RewriteEngine On
RewriteRule ^/?$ http://www.main-site-new.com [R=301,L]
Note: clear browser cache then test.

Redirect subdomain and path to main domain with same path

I have recently stopped using a subdomain for my blog, i need to forward all links that use that domain to the same link path but on my main domain.
Example being:
blog.example.com/blog/sales/blog-title
needs to redirect to :
www.example.com/blog/sales/blog-title
I can redirect the subdomain itself so
-blog.example.com/
goes to
www.example.com/blog
but as soon as I try to redirect a page it doesn't redirect and doesn't load. Can anyone shed some light on it? I currently have this in my htaccess for my main redirect:
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule (.*) http://www.example.com/blog/$1 [L,R=301]
I have other subdomain redirects going on but none wildcard redirect.
In your example, you want to redirect from
blog.example.com/blog/sales/blog-title
to
www.example.com/blog/sales/blog-title
But in your rule, you insert another subdirectory blog in the substitution part, which gives
www.example.com/blog/blog/sales/blog-title
instead, and an error 404 as a result.
To redirect from one domain to another with the exact same request path, use
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R]
When it works as it should, you may replace R with R=301. Never test with R=301.

Redirect to home if url contains string with a dot using ,htaccess

My Wordpress site is under attack. A bot sends queries that crashes my MySql. All of them contains "/?s=" and ".pl" in the url, so, I whant to redirect to home if someone is trying to access them.
Example:
If www.example.com/?s=xxxxx.pl redirect to www.example.com/
If www.example.com/?s=123.pl redirect to www.example.com/
Can you help me? I couldn't find how to edit .htaccess to solve it.
You could use a more robust system like mod_qos to stop these types of attacks then it will automatically block them by IP and other ways.
However you can do something like this with rewrite rule and then it will get 403 forbidden.
RewriteEngine on
RewriteCond %{QUERY_STRING} s=(.+)\.pl
RewriteRule ^ - [F]
if you still want to redirect to home page you can do this.
RewriteEngine on
RewriteCond %{QUERY_STRING} s=(.+)\.pl
RewriteRule ^ /? [R=301,L]
Let me know how this works for you.

301 Redirect Not Working .htaccess

I have the following redirect code in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301,NC]
</IfModule>
# END WordPress
AddHandler php-stable .php
I'm trying to redirect all traffic from olddomain to newdomain. Not that the new domain does use "www".
When clicking on an old link elsewhere on the net that would refer to a post, redirect does seem to happen but it does not add in the correct slash after the domain name.
Here is what happens, the link to my old domain is as such on a website (just posted by someone):
http://olddomain.com/some-blog-post/
When I click on that link in a browser, it actually opens up:
http://www.newdomain.comsome-blog-post/
The slash between the domain and the blog post is missing.
Also, if it helps I'm using MediaTemple as my host for the old domain and I'm modifying my .htaccess file in
domains/olddomain.com/html/.htaccess
Anyone have any idea out there? Thanks a ton, I'm stuck.
Ok I just checked on this a little more and it turns out everything is working fine with the .htaccess code I pasted above.
My web browser was caching aggressively and I just had to dump the cache (or use private browsing).
See here for more debugging tips:
Tips for debugging .htaccess rewrite rules
maybe this is better:
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]
It matches both the subdomain and request URI and merges them with the new domain. If there is no subdomain, it proceeds to match the request and will redirect without a subdomain.

redirect from one domain to another but keep requested page

I want to create a redirect for one of my domains to another.
I followed what reported in this question, and it works just fine.
The problem I'm having now is that I want to redirect www.example.com/some-page to
www.new-example.com/some-page.
Right now, www.example.com/some-page just redirects to www.new-example.com, this could be confusing for users who created bookmarks to some parts of the website.
Both URLs link to the same webhosting, and I'm running a litespeed server.
Add the following to your .htaccess file:
RewriteCond %{HTTP_HOST} !^yourdomain.com [NC]
RewriteRule ^ http://yourdomain.com%{REQUEST_URI} [L,R=301]
Edit: yourdomain.com should be the domain you wanna redirect to.

Resources