Which .htaccess file should I be using for 301 redirects? - .htaccess

This is one of those super-simple questions that I can't seem to google an answer for, so apologies in advance.
When I ftp into my (shared) server, I have a file structure like this:
Root (/)
/public_html
/newdomain.com
I had an old website that lived in /public_html, it had heaps of content and excellent SEO. We changed our name and our domain (which lives in /newdomain.com, a folder inside /public_html), and set 301 redirects from all the old content to the new website.
I tried doing this myself, but it didn't work at all, so I got my host's techsupport to do it for me. There are several .htaccess files on my server though, and I don't know which ones are actually effective and which aren't.
Root has its own .htaccess file
public_html has its own .htaccess file
/newdomain.com DOESN'T have its own .htaccess file
Redirection 1 (currently is in both root and public_html's .htaccess files, and works)
I want to redirect http://olddomain.com/whatever -> http://newdomain.com/whatever (I've currently got each individual page doing its own separate 301 versus a single rule doing this). Achieved with Redirect 301 /article-name-here/ http://www.newsite.com/article-name-here/
Redirection 2 (currently is in both root and public_html's .htaccess files, and doesn't work).
I also want to do some internal redirections of http://newdomain.com/oldpage.html -> http://newdomain.com/newpage.html. I've tried redirection public_html's .htaccess file like so:
Redirect 301 http://newsite.com/badpage.html http://newsite.com/goodpage.html
But it's not working. Do I need to set up a new .htaccess in the newsite.com folder on my server? Or am I just completely missing the mark here?

Redirection 1
To redirect everything, just remove the article name:
Redirect 301 / http://www.newsite.com/
Or if you don't want to redirect the root (i.e. requests for /), then:
RedirectMatch 301 ^/(.+)$ http://www.newsite.com/$1
Redirection 2
If the /newdomain directory is the document root for http://newdomain.com/, then you'll need to create a new htaccess file there and include:
Redirect 301 /badpage.html /goodpage.html

Related

301 redirect from one domain to another but only for specific pages

We are rebuilding a website for a client from ASP to WordPress. This website will have a different domain, url structure, and file extension. I am only just getting my head around htaccess 301 redirects, and I know enough that I can't do the following:
Redirect 301 http://www.site1.com/about_us.asp https://site2.com/about/
Redirect 301 http://www.site1.com/art-specs/ https://site2.com/specs/
Redirect 301 http://www.site1.com/page/product1/ https://site2.com/product1/
There are about 12 links in total that need to be redirected, and I want to make sure that it is done right the first time as a client's SEO rankings are on the line.
Is there a variation of the above format that I could use? Or a rewrite rule that needs to be done first? Any help (and explanations) would be greatly appreciated!
After looking more into it, I realised that the htaccess file shouldn't need anything other than relative access to the original domain.
i.e. You shouldn't need to declare: http://www.site1.com/about_us.asp since the server and domain should be configured in such a way that /about_us.asp means the same thing.
So the correct answer would be to:
[1] Configure the server (in my case cPanel) by having the original domain added as an addon domain (e.g http://www.site1.com/).
[2] In the htaccess file I would add each of the 301 redirects to the htaccess file:
Redirect 301 /about_us.asp https://site2.com/about/
Redirect 301 /art-specs/ https://site2.com/specs/
Redirect 301 /page/product1/ https://site2.com/product1/
...for each redirect
[3] And finally, adding the following to the bottom of the htaccess file will catch everything else and redirect them to the home page:
RedirectMatch 301 .* https://site2.com

GoDaddy and .htaccess 301 redirects in subfolder

My website points to a sub-sub folder on the webroot on GoDaddy (/mywebsite/mywebsitesfolder). Apparently from my research, you cannot use a 301 redirect in an .htaccess file if it's in a subfolder of the webroot on GoDaddy. I've tried so many different things. My custom ErrorDocument statements work and bring up the error documents within this particular subfolder, of which my domain points to. What should I do to make this work aside from moving my website completely out of GoDaddy and to Bluehost?
rewriteengine on
redirect 301 /oldfile.html http://mynewsite/newfile.php
ErrorDocument 404 /.errordocs/404-missing.php
I am answering my own question here but it works like this. In order to successfully do a 301 redirect for domains pointing to subfolders on GoDaddy, you have to put the complete file path where you want the server to LOOK for the old file. I was getting confused because my old site was in a different folder and I was telling it to look in that old folder instead of the new folder I pointed the domain to. So my code would actually look like this instead of what's above in my question.
redirect 301 /mywebsitesfolder/oldfile.html http://www.newsite.com/newfile.html
No matter how many subfolders down in the webroot it is, you have to put the whole filepath. Im my case, my redirects now look like this:
redirect 301 /mywebsitesfolder/mywebsitesnewfolder/oldfile.html http://www.newsite.com/newfile.html

htaccess redirect subfolder to another without using root htaccess

I've been searching all day for a solution.
So a friend of my used the horrible web builder to build a basic website in the root directory. For whatever reason, web builder sticks its files in a /site subfolder and it holds the root htaccess hostage. Any changes to web builder overwrite the htaccess.
Now, she has two subfolders. sub1 and sub2. We need to get any attempt to access sub1 to redirect to sub2. I am used to doing this by putting the following code in the root htaccess:
# Enable rewriting.
RewriteEngine on
RewriteRule ^/?sub1/(.*)$ /sub2/$1 [R,L]
But as said, the web builder will erase all of that any time she publishes any changes. I put a new htaccess file in sub1 with the following text:
Redirect 302 / http://example.com/sub2
That redirected the subfolder, but not correctly. It actually redirected to http//example.com/sub2sub1 for some reason that I can't figure out.
I feel like the solution is right in my face but I can't figure it out.
You can use RedirectMatch Directive.
RedirectMatch 302 /sub1/(.*) /sub2/$1

Implementing 301 redirects in sub-folder of Silverstripe

I am trying to create 301 redirects from a silverstripe subdirectory to a new site/subdomain on different server, can't seem to make it work!
So need www.researchnutrition.com.au/practitioner redirecting to www.practitioner.researchnutrition.com.au
along with a number of other urls within the practitioner subfolder specified to new pages on new domain.
Have tried several combinations or rewrite/redirect rules but nothing seems to be taking effect.
Also should the .htaccess file where I make the changes need to sit within the root folder or within the /practitioner folder.
Thanks so much in advance.
You can use .htaccess file in the practitioner folder:
RewriteEngine on
RewriteRule (.*) http://www.practitioner.researchnutrition.com.au/$1 [R=301,L]
Another option is just creating a redirection page in the cms and redirecting that to an external url (which is, despite the option saying "redirect to internal page", possible).

Redirect custom URLs via htaccess

I have my own domain:
http://cesarferreira.com
and I wanna make
http://cesarferreira.com/github
point to
https://github.com/cesarferreira
Without having to make a /github/ folder with an index.html with a redirect for each page I own (facebook, twitter, pinterest, etc)
Is there a way like for example htaccess catchig *.com/github and pointing to a given static url?
Thanks a lot
If your document root serves -
http://cesarferreira.com
you can put a redirect in .htaccess like -
Redirect /github https://github.com/cesarferreira
Take a look at URL rewriter 'http://httpd.apache.org/docs/2.0/misc/rewriteguide.html'. That should be able to do everything you want and more.
As long as it is enabled in apache then you can use it in .htaccess files also.
You can use mod_alias:
Redirect 301 /github https://github.com/cesarferreira
Or if you only want github to point only to the folder:
RedirectMatch 301 ^/github https://github.com/cesarferreira
You can put that in the htaccess file of your document root.

Resources