How can I redirect 301 all error pages to sub folder? - .htaccess

I want to redirect 301 with htaccess file my broken URLs. What do I mean? If visitor come to this link: http://mydomain.com/blablablablablablablabla34234234.php he probably will get an 404 error so I want my "404 page" will be in sub folder. like this:
http://mydomain.com/somenonpage.php to http://mydomain.com/sub/somenonpage.php
http://mydomain.com/blablablablabla to http://mydomain.com/sub/blablablablabla
etc.

If you want to have a custom 404 page you do not do a 301 Redirect, 301 is to redirect an old link to a new URL. Say you had a popular post but decided to change the url for SEO reasons but the old url was heavily backlinked. You would then use a 301 to make sure that those backlinks got associated with your new url.
To make a custom 404 all you need to do is edit your http.conf file and add a path to your custom 404 page. By default I think the file belongs in your root directory named 404.html.
If you need the fileto have a different name or location you can edit the file path at the line beginning line ErrorDocument 404 /404.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

Can I keep robots.txt in a contextpath and give a 301 redirect?

My website uses a contextpath (eg: www.example.com/abc). The robots.txt is available at www.example.com/abc/robots.txt and I have given a 301 redirect in webserver to redirect www.example.com/robots.txt to www.example.com/abc/robots.txt.
My question is whether the search engines be able to read the robots.txt file since it has a 301 redirect?
Found that the search engines are honoring the 301 redirect and reading the file from the subfolder.
Robots.txt should be on root level
https://example.com/robots.txt - Correct
https://blog.example.com/robots.txt - Correct
https://example.com/abc/robots.txt - Not Correct
https://blog.example.com/abc/robots.txt - Not Correct
If it is on sub directory/sub folder then it will return 404 error(Because they make calls only on root directory), and Google will ignore your robots.txt completely if it is return 301 or 404 error.

301 Redirect old site URL's in new Drupal installation

I'm looking to 301 redirect an URL from a old version of a site no longer being used to a new URL that has been created in fresh Drupal installation.
Old URL: /198/our-software/
New URL: /services/software-development/
In the .htaccess located in the root directory of Drupal I have added the following:
redirect 301 /198/our-software/ http://www.domain.com/services/software-development
The redirect is working to some extent, it sends the user to a url like below with a query string appended to the end of it, which results in a 404 error:
http://www.domain.com/services/software-development?q=198/our-software/
I have tried placing the redirect at both the start and end of the .htaccess file both result in a 404 page not found error.
Do I need to use a more complex redirect to get around Drupals URL rewrite?
NOTE: I'm using the Pathauto module.
Rather than edit the .htaccess directly, just install the Path Redirect module which has that exact functionality built in.
Note that the Path Redirect module is only available for Drupal 6 (as of 2/22/12)
I got it working with using "RewriteRule" instead, AND (important!) removing the leading slash in the source URL, so in your case:
RewriteRule 198/our-software/ http://www.domain.com/services/software-development [R=301,L]

Redirecting bad url links from external sites with .htaccess

I have two inbound links that land on 404 pages because the links are squiffy. I have tried 301'ing these links as normal but without success, I believe because of the characters used from this external URL.
Column<u>Radiators.html
is the page suggested on this external site and
Column_Radiators.html
is the actual page.
also
Bath%3Cu%3EFiller.html
on the external site and
Bath_Filler.html
on our actual website.
How can I succesfully redirect these pages?
Simply add these lines in your .htaccess
redirect 301 /Column<u>Radiators.html /index.html
redirect 301 /Bath<u>Filler.html /index.html
Note: I use index.html as the default redirection but you can use a custom 404.

Resources