Need Top Level 301 Redirect but subdomain has same name as redirect? - apache

I have a main site with multiple subdomains below it:
so:
example.com
products.example.com
books.example.com, etc.
I'm trying to create a redirect like:
RedirectMatch 301 ^/products/catalog/(.*) //products.example.com/$1
But this doesn't work because at "products" is a subdirectory/subdomain so it just creates a redirect loop at //products.example.com
But This works (because /store isn't //store.example.com/)
RedirectMatch 301 ^/store/catalog/(.*) //products.example.com/$1
I tried experimenting with a rewritecond to have it only redirect if the url contains the root domain, but that didn't work either.
If a page like:
//products.example.com/products/great-thing.html exists and is a subdomain under a main domain installed in a folder "products" (i.e. /public_html/products/...)
-- how would you redirect from:
//example.com/products/great-thing.html to
//products.example.com/products/great-thing.html
I hope that makes sense!
I should add that this site is hosted on a managed cloud/vps - so there may very well be something in their setup that is causing trouble.
But at the end of it there seems to be a problem creating redirects when a folder/subdomain exists with the same name...

This was hard to track down and may have been more of a processwire cms issue than a straight apache redirect issue, but in case it is helpful to someone, what worked was:
Top level (i.e. root domain site) needed for
//example.com/products/great-thing.html to //products.example.com/products/great-thing.html
RewriteRule ^(.)/products/(.) https://products.example.com/products/$2 [R=301,L]
which should have gone to:
https://products.example.com/products/great-thing.html
, but instead created a loop which never resolved.
But it does work when on the subsite (processwire) this was added:
RewriteRule ^products/(.*) ^/products/$1 [R=301,L]

Related

.htaccess 301 redirect old to new domain, from hosting root to subfolder, working but why?

The scenario: I've moved a WordPress website to a new domain and want to 301 redirect all the pages from the old domain to the new domain. Both sites are on the same hosting account running Apache. The old site is at the root level (public_html), and the new site is in a subfolder (below/inside the root).
I've managed to make this work, but I'd like to learn and understand why it works. So below is a quick overview of my 'journey' and solution, together with three specific questions.
First I tried to do the redirects like this (code added to the root .htaccess file):
# 301 Page Redirects - not working - causes redirect loop
redirect 301 / https://new-domain.com/
redirect 301 /services/ https://new-domain.com/services/
redirect 301 /recipes/ https://new-domain.com/recipes/
But this causes a redirect loop. I'm guessing because the .htaccess file with these rules is at the root level and therefore also affects the subfolders.
Question 1: Is my assumption above about the reason for the redirect loop correct?
Then I tried to be more specific and put this code in the root .htaccess file instead:
# 301 Page Redirects - not working - does nothing at all - not sure why
redirect 301 https://old-domain.com/ https://new-domain.com/
redirect 301 https://old-domain.com/services/ https://new-domain.com/services/
redirect 301 https://old-domain.com/recipes/ https://new-domain.com/recipes/
I was hoping the above code would do the trick, because it's more specific about the old domain. My thinking was that it specifies the old domain exactly and so would circumvent the redirect loop. But instead this code seems to have no effect at all. The redirect loop was gone, but now no redirects were happening anymore at all.
Question 2: Why would the above code not produce any redirects at all?
Then I found this answer and applied the code from that, which works perfectly and creates all the redirects. Plus it's much more elegant than my previous attempts above. This is the code:
# 301 Redirects from old-domain.com to new-domain.com - THIS CODE WORKS - Yay!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule (.*)$ https://new-domain.com/$1 [R=301,L]
Question 3: Why does this code not cause any redirect loops when I place it in the root .htaccess?
I realise I'm copy/pasting code without fully understanding why it works. So I'd love an explanation in simple terms about these behaviours. Thank you.
Answer 1:
From the info that you have provided, I would say no. You did not specify if the new-domain.com website is configured (in apache configuration) with its document root being public_root or public_root/subfolder (judging by the described behaviour I would say it is the former). In that case, when you request https://old-domain.com/anything, the server will (because of the unconditional redirect in your first rule) respond with redirect to https://new-domain.com/anything. Client browser will then request that URL and it will hit the same Apache and same .htaccess, which will again result in the same redirect, causing the loop.
Answer 2:
Redirect syntax:
Redirect [status] [URL-path] URL
The old URL-path is a case-sensitive (%-decoded) path beginning with a
slash.
In your rule, you are specifying [URL-path] as https://old-domain.com/, which is wrong: it can be /, /services/, or /recipes, but not https://old-domain.com/ or https://old-domain.com/services/. The request [URL-path] does not match [URL-path] specified in your rule, so redirect never happens.
Answer 3:
This basically does the same thing as your first rule in Answer 1., with one important difference: the server will respond with redirect only if the hostname in request (or to be more precise, the content of the Host: header in request) is equal to old-domain.com or www.old-domain.com, which will prevent the loop since the second request from the client will use new-domain.com hostname.
Also, from the above, seems like your "new" website in subfolder will never be served: either if old-domain.com or new-domain.com is requested, the site from public_html folder will be shown (and only the hostname in clients browser address bar will change).

htaccess redirecting website.com/folder/content to subdomain.website.com/content

I recently moved a Wordpress blog from website.com/wordpress to help.website.com. Now I want to redirect the links to the old adress to the new adress.
I deleted everything from the /wordpress folder except the .htaccess. This file has the following code:
RedirectMatch 301 (.*) http://www.help.website.com$1
It redirects my old links but I don't know how to remove the /wordpress from them.
For example if I access website.com/wordpress/article-categories/example-article/ it sends me to help.website.com/wordpress/article-categories/example-article/, but I want to get to help.website.com/article-categories/example-article/ instead. How do I achieve this?
Try
RedirectMatch 301 ^/wordpress/(.*)$ http://www.help.website.com/$1
This will match the folder literally without capturing it.

Site Redirection with htaccess creates an infinite loop

Currently we have a number of sites hosted in one GoDaddy account. Each site is inside a separate folder and their respective domains are bound to those folders. The problem is that our main site is in the root of the host and our primary domain is linked to this root. The problem with this setup is that if for example, one of my other sites is in a folder called "secondsite", I can reach the website by going to www.secondsite.com (which is fine) but ALSO by going to www.mainsite.com/secondsite, which we absolutely not want.
The idea is to move all the files of the main site to a folder of their own (let's call it "mainsite"). When I talked to GoDaddy they told me to do a 301 redirect to that folder. I have never worked with .htaccess but I looked up how to redirect and found that I needed to write this:
Redirect 301 / http://mainsite.com/mainsite
However if I do that when I try to access the website I get infinite redirection: http://mainsite.com/mainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsite
I've looked online and tried to use other solutions, like
RedirectPermanent / http://mainsite.com/mainsite
but the effect is the same.
Not sure what I'm doing wrong.
When using Redirect, you're linking path-nodes together, So:
Redirect / /abc/
means, anything starting with / will go to /abc/, e.g.:
/ -> /abc/
/foo -> /abc/foo
/1/2/3/4/5 -> /abc/1/2/3/4/5
And thus, since you're redirecting back to the same host, the / captures everything and you've got an infinite loop.
Try using either RedirectMatch:
RedirectMatch 301 ^/(?!mainsite)(.*)$ /mainsite/$1
or us mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$ [NC]
RewriteRule ^(?!mainsite)(.*)$ /mainsite/$1 [L,R=301]

Apache redirect everything under a folder except the folder itself

At some point in the past, our site had a structure like www.example.com/departments/it or www.example.com/departments/asdf. Then, the the paths changed to something like www.example.com/it or www.example.com/asdf, getting rid of the 'departments'.
There is content that now lives under the /departments/ path, so using the following rewrite rule doesn't work
RewriteRule ^/departments/(.*)$ /$1 [R=301,L]
Because while it rewrites everything under departments, departments itself gets redirected back to the site root.
How do I prevent this so that if I go to www.example.com/it it works, www.example.com/departments/it redirects to www.example.com/it, and if I go to www.example.com/departments/, I get the department information?
Paste this in and give it a try
RewriteRule ^/departments/it$ /it/$1 [R=301,R,L]

Apache mod_rewrite help with Wordpress

I administer my wife's site, namelymarly.com. Up until last week, the root page of the blog was namelymarly.com/blog/.
Last week I changed it in the WP settings to be namelymarly.com.
WP created the new htaccess file, and I moved the index.php to the root directory (but left the WP folder where it was in the /blog/ directory), as instructed. Everything is working great except for one very important thing:
When you type 'namelymarly.com/blog/' into a browser now, you get a 404 error.
All other URLs, when they include the '/blog/somethinghere', will redirect properly to '/somethinghere.' It's only when there's nothing after '/blog/' that there's a problem.
I tried adding this rule but it still redirects to the 404 page:
RewriteRule ^/blog/$ /index.php
Any suggestions/help?
install "Redirection" and then add a 301 redirect from namelymarly.com/blog/ to namelymarly.com
Did you follow these diections?: Moving WordPress « WordPress Codex
You don't need the redirection plugin. Wordpress handles redirects if you regenerate permaliks. If you have to, use this in .htaccess before the Wordpress rewrite block:
Redirect 301 /blog http://namelymarly.com
But first, be sure you've reset your permalinks in Dashboard/Settings/Permalinks and make sure that copy the changes to .htaccess yourself and that there is only the most recent - the last - rewrite block in the file (WP has a habit of adding more and more rewrite blocks to .htaccess).
And check the URLs of your other URLs in the post/page editor and see if they contain /blog/