301 Redirect a folder to a single page - .htaccess

I have a folder on my website that has been superceded and I want to redirect an attempt to access any file in this folder to the current home page.
I have read many questions here but none seem to do exactly this.
I have tried various things in .htaccess but it seems to always append the filename to the redirect address.
redirect 301 old/$ www.example.com/index.html
redirect 301 old/ www.example.com/index.html
redirect 301 ^old/ www.example.com/index.html
I have seen various mentions of RewriteRule. Do I need to use that?

You can use this code in your /old/.htaccess file:
RewriteEngine On
RewriteRule ^ http://www.example.com/index.html [L]
OR from root .htaccess use:
RewriteEngine On
RewriteRule ^old(/.*)?$ http://www.example.com/index.html [L,NC]

Related

301 Redirect root without cascading new root directory to all other 301 redirects

I have successfully redirected the root of my site to a sub-directory in another site:
#Redirect Root to subdirectory on example.com
RewriteEngine On
RewriteRule ^$ http://example.com/subdirectory [L]
I'm running into trouble with all of my other redirects. The sub-directory of the new root (/subdirectory) is getting inserted into my redirects before each path, which is resulting in broken redirects.
Example:
Redirect 301 /contact-us/ https://www.example.com/company/contact.shtml
tries to point to
https://www.example.com/subdirectorycompany/contact.shtml
In a nutshell, I can either get the root working, or I can get the redirects working -- but not both. Can anyone please lend some insight?
I have read a bunch of posts, which is how I have gotten as far as I did, but I could use some help, please and thank you.
Don't mix Redirect directive and RewriteRule as they are from different Apache modules and are invoked at different times.
It is better to have all your rules like this:
RewriteEngine On
# all specific 301 redirects go here
RewriteRule ^contact-us/?$ https://example.com/company/contact.shtml [L,NC,R=301]
#Redirect Root to subdirectory on example.com
RewriteRule ^$ http://example.com/subdirectory [L,R=301]

htaccess how to redirect all pages in the directory to index page

I want to redirect from all pages to index page in the directory.
And wrong addresses too.
http://site.com/directory/anyword move to http://site.com/directory/index.html
I did in .htaccess RedirectMatch 301 /directory /directory/index.html
But I need except directory/thankyou.html
It does redirect but doesn't open index.html - resulted in too many redirects
May be I can try with 404.
You need to exclude index.html from your match, otherwise you get an infinite loop where directory/index.html redirects to itself over and over.
Unfortunately my knowledge of writing .htaccess files is basically nonexistent so I can't provide an example.
Mod Rewrite?
RewriteEngine On
RewriteCond %{REQUEST_URI} !index\.html$
RewriteRule /directory/.+ /directory/index.html
You can redirect all pages or subdirectory to the index page.,
try this-
root/.htaccess
enter code here
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^(.+?/)?home/?$ $1/index.php [L,NC]

How to Permanent Redirect a Directory That No Longer Exists (via .htaccess)

I am having trouble redirecting an entire directory that no longer exists on our server.
All variations of the following are not working and I simply get a 404 Page Not Found.
.htaccess file looks like this:
redirect 301 /non_existent_directory/ http://my.website.com/existent_directory/
Is it possible to use the Redirect 301 directive for this? Or is this only solvable by mod_rewrite?
Thanks
I even tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com\/non_existent_directory\/$ [NC]
RewriteRule ^(.*)$ http://my.website.com/existent_directory/ [R=301,L]
No luck...
From the Redirect documentation, I would say
Redirect 301 /non_existent_directory http://my.website.com/existent_directory
or
Redirect 301 /non_existent_directory /existent_directory
should work, provided you are allowed to use this in a .htaccess file. See also Troubleshooting .htaccess files. You should test without 301 though, to prevent caching of bad redirects by the client.
If this doesn't work, you can try a RewriteRule of course
RewriteEngine On
RewriteRule ^/?non_existent_directory(.*)$ /existent_directory$1 [R,L]
But this is equivalent to the above Redirect directive.

How to redirect a specific page using htaccess

I've looked all over for the htaccess code to redirect a single page and haven't had any luck with many solutions.
Basically I need to redirect this:
/example/my-stuff/
to:
/example/home/
but I don't want any other pages except for /my-stuff/ to be redirected. Eg. these pages should not be redirected and kept the same.
/example/my-stuff/a-page
/example/my-stuff/anything
In the htaccess file in your document root, you can add either this:
RedirectMatch 301 ^/example/my-stuff/$ /example/home/
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]

Problems with htaccess Redirect (Page on one domain to same page on another)

I'm trying to redirect a few pages to a new domain and I have done this before but for some reason I can't get the code to work.
RewriteEngine On
Redirect 301 http://domain.com/page1.html http://domain2.com/page1.html
Can anyone see where I'm going wrong?
In .htaccess file the below code will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Since you say you only want to direct a FEW of your pages and not all of them, you can do:
RewriteEngine On
Redirect /~myaccount/oldpage.html http://www.newsite.com/newpage.html
You specify the path to the page on your current server followed by the URL to redirect to.
OR you can do:
RedirectMatch 301 ^/oldpage\.html$ http://www.newsite.com/newpage.html

Resources