redirect one page to another page via htaccess

Discussion in 'Coding Help' started by PMonaghan, Aug 6, 2013.

  1. PMonaghan

    PMonaghan New Member

    Hi, I am new to this forum - and any forum. I need help learning how to do a redirect for specific pages of a website. I have the code:
    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} * [nc]
    rewriterule ^(.*)$$1 [r=301, nc]

    I know that I am supposed to replace and with the actual domain names, but I don't want to redirect the domain, I want to redirect pages. I have tried to find help on the Internet (and went to the hosting company), but I cannot find the help I need to make this work. Can anyone help me? I want to redirect someone typing in the old or former name of the page to the newly-named page on the same website. Thanks so much!
  2. Satanta

    Satanta New Member

    When you say 'pages' it does open up the question of how related are these pages? If there's a pattern to these pages (e.g. if they all reside within a single directory) it would be easier to set up a rewriecond for them than simply individual page links.

    If you want to do all individual redirects, you can simply use...

    redirect 301 /olddirectory/oldpage.htm
  3. PMonaghan

    PMonaghan New Member

    Hi Satanta, Thank you for responding. The structure of my site is setup in directories within the root directory. Then the pages are all index.html. I have changed the directory names to be more seo-friendly, so I need to redirect visitors from the former to the new. All of this is within one website. I am modifying the .htaccess file. I tried the line you gave me above (I put it in the .htaccess file), but it does not work. Perhaps the redirect line you gave me above goes somewhere other than the .htaccess file? I have six 'pages' that I need to redirect because I renamed the directories. Thanks in advance for your help.
  4. blue4ever

    blue4ever New Member

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
    RewriteRule (.*)$$1 [R=301,L]

    This allows the old site being accessed as www or non www to be redirected to

    If your site is set-up to be a non www, then replace the second last line with:

    RewriteRule ^(.*)$$1 [L,R=301]

    This will redirect to
  5. Satanta

    Satanta New Member

    Given you're transferring directories, you're probably better off using rewritecond's to do it. You said that it's always a single index.html file at the end of it, but due to technical issues it could well happen that you'd need to redirect more than that.

    blue4ever gave some good advice about using a rewrite rule to remove issues around the www and non-www versions of your site, but you might well have this taken care of (with a plugin or the likes). If not, do include it.

    So, with that in mind, you'd be looking at rules like....

    Options +FollowSymLinks
    RewriteEngine on
    //301 Redirect Entire Directory
    RedirectMatch 301 /olddirectory/(.*) /newdirectory//$1

    (In order to make sure you're rewriting to/from all the right files/directories, you're best to use a site map. You can't crawl the site for your old links now [they're gone], but you can still grab all of your new ones which should help a little. Lots of tools out there that will crawl your site for you, personally I like Xenu or Screaming Frog SEO Spider)

Share This Page