Redirect subdomain and path to main domain with same path - wordpress

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.

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.

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.

Enable https using Rewrite Rule for specific URLs

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.

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.

How do I modify my .htaccess file to redirect a URL for one domain only, and not rewrite other domains hosted by the same server

I switched blogging software (MT to WordPress) on a site and need to redirect requests to http://www.domain1/atom.xml to http://www.domain1.com/feed/atom.
I was using a simple Redirectmatch rule, but realized that it was also redirecting requests made to another site (domain2), that is is hosted by the server, in a subdirectory of domain1, which I do not want to happen (its feed is still at http://www.domain2.com/atom.xml).
How do I get the redirect to only occur for domain1?
I was trying to do the following, but it didn't work.
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^/atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
Am I close?
Thanks,
Rich
If you don't do any rewriting for domain2 then a quick fix would be to create a .htaccess file inside its root folder and disable rewriting with RewriteEngine off.
Otherwise you are on the right path with the RewriteCond, it should do the trick. Have you tried adding $ at the end (RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]) / any misspelling / www. vs no www.?
I figured it out, but I'm not sure why exactly this works. I moved my .htaccess to be:
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
I removed the slash in front of "atom" in the RewriteRule.
I would think I should have the slash, as I'm tyring to redirect http://www.domain1.com/atom.xml .
It's at the root of the domain...
Oh well. Can anyone explain why this works? Is the string passed to the pattern matching not contain the starting slash?
Thanks,
Rich

Resources