Help 301 Redirection rule

Posted on 2011-10-28
Last Modified: 2012-05-12

The normal URL structure on my site is:

by accident I got lot of URLs indexed in Google this way:

The wrong URLs are leading to a page with error.

I need to write some 301 redirection rule on my .htaccess to redirect ALL links like


The attached code is the content of my .htaccess inside /search/ folder.

Please help, help!

Thank you in advance.
RewriteEngine On
RewriteBase /search/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.) /search/index.php?query=$1 [L]

Open in new window

Question by:Fernanditos
    LVL 4

    Expert Comment

    Have you tried redirecting it this way?  Do you know how many web pages are being indexed that way?

    # Redirect old file path to new file path
    Redirect /olddirectory/oldfile.html

    Open in new window

    LVL 3

    Expert Comment

    i want to say you answer but you made some mistakes.
    SEO tip: instead or + using - it's better.

    for redirecting to add following code to .htaccess
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www..*
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
    RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]
    RewriteCond %{REQUEST_URI} /search/.*
    RewriteRule search/(.*) search/pdf/$1 [R=301,L]

    Open in new window

    LVL 26

    Accepted Solution


    You've a typo on line 6 of you rewrite rule block, as "(.)" asks the rule to just the FIRST character, not a whole string (book+title), for the Rewrite. So for a start try changing line 6 to look something like:

    1) Take the WHOLE URL path after the "/search/", and 302 rewrite it to the index call.
    RewriteRule (.*) /search/index.php?query=$1 [L]

    Open in new window

    2) Take the WHOLE URL path after the "/search/", and 301 redirect it, adding a /pdf/ prefix.
    RewriteRule (.*) /search/pdf/$1 [L,R=301]

    Open in new window

    3) Take the URL path after "/search/", up to the first "/", and 301 redirect it, adding a /pdf/ prefix.
    RewriteRule ([^/]+)$ /search/pdf/$1 [L,R=301]

    Open in new window

    This article I wrote a wile back covers the basics of rewriting, may be of some use.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to dynamically set the form action using jQuery.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now