How to redirect a specific page using htaccess - .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]

Related

301 Redirect a folder to a single page

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]

Redirect if a URL contains this string

I don't know much about .htaccess, but I'm trying to help a friend who recently moved his blog to Wordpress.
We need to redirect the OLD archive pages like this:
www.domain.com/2010_04_01_archive.html
www.domain.com/2010_04_02_archive.html
www.domain.com/2010_04_03_archive.html
to NEW archive pages like this:
www.domain.com/2010/04/01
www.domain.com/2010/04/02
www.domain.com/2010/04/03
I've tried everything I can find using htaccess redirect and rewrite, but again, I don't really know what I'm doing with htaccess!
Thanks so much for your help,
Amanda
OK tried this:
Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([0-9]{4})([0-9]{2})([0-9]{2})_archive.html$ /$1/$2/$3 [L,R=301]
in .htaccess in the very top level folder of my site. Still, when I go to http://www.bikermetric.com/2010_04_01_archive.html, it doesn't redirect.
Just tried this too:
RedirectMatch 301 ^/([0-9]{4})([0-9]{2})([0-9]{2})_archive.html$ /$1/$2/$3
Still nothing.
You can use mod_alias or mod_rewrite here. You'll want to stick with using mod_rewrite if you already have rewrite rules (stuff that look like RewriteEngine or RewriteRule):
mod_alias:
RedirectMatch 301 ^/([0-9]{4})_([0-9]{2})_([0-9]{2})_archive.html$ /$1/$2/$3
mod_rewrite:
RewriteEngine On
RewriteRule ^([0-9]{4})_([0-9]{2})_([0-9]{2})_archive.html$ /$1/$2/$3 [L,R=301]
You'd want to add it to the htaccess file in your document root.

.htaccess 301 Redirect dynamic .cfm pages

I have pages indexed by Google such as sample.com/product-detail.cfm?id=532323. There are a lot of these pages. These pages no longer exist and I would like to redirect all of them to the home page of our new site. Can anyone suggest how to redirect all of the pages without having to do each one individually?
All of them should go to the home page
Then you can just check for /product-detail.cfm and ignore the id= param in the query string. You can do this with either mod_alias or mod_rewrite by adding these directives in the htaccess file in your document root:
Using mod_alias:
Redirect 301 /product-detail.cfm /
Using mod_rewrite:
RewriteEngine On
RewriteRule ^product-detail.cfm$ / [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

Redirect all but some to new URL?

I am moving site and I want to do a 301 redirect on all but some of the URLs,
Like this:
oldsite.com/* -> www.newsite.com
oldsite.com/specific/article/to/redirect -> www.newsite.com/fancy/blah
So there are a few things I want to redirect to specific pages but all the rest should just goto root, how can this be done in .htaccess?
Add the following to the .htaccess file in the root directory of your old site.
#place your specific redirects first
Redirect 301 /specific/article/to/redirect http://www.newsite.com/fancy/blah
RewriteEngine on
#then your general redirect all to new site last
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [L,R=301]
There's RedirectMatch if you've got only one URL that needs to be exempted.
RedirectMatch permanent !/specific/article/to/redirect http://www.newsite.com
for multiple URLs, you'd probably be better off with mod_rewrite and an external rewritemap that lists the urls to be exempted.

Resources