301 redirect only if URI doesn't contain specific string

12,302

Solution 1

This is what you need :

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !web1

RewriteRule  ^(.+)$ http://new-website.com [L,R=301]

Solution 2

A colleague of mine, found a solution:

RewriteCond %{REQUEST_URI} !^/web1(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^([\w/]*)$ http://www.new-website.com [L]`
Share:
12,302
MatDK
Author by

MatDK

Updated on August 17, 2022

Comments

  • MatDK
    MatDK over 1 year

    I have a drupal website which has been split up to two separate websites, and now I need to setup some rewrite rules in order to drive traffic to the new site.

    Original website looks like this:

    http://www.website.com (frontpage)
    http://www.website.com/web1/subpage1 (subpage)
    http://www.website.com/web1/subpage2 (subpage)
    http://www.website.com/subpage3 (subpage)
    http://www.website.com/subpage4 (subpage)
    

    All references to subpages that are not in the web1-category have been removed from the website, but the pages are still published and they still show up in Google.

    What I need is a rewrite rule that redirects from "website.com" to the frontpage of "new-website.com" if the user tries to access a page that is not the frontpage and not in the web1-category.

    I suppose a rewrite-rule checking for the string "web1" in the URI would be the answer to my problem, but unfortunately I have no idea how to write the syntax.

    Any help would be appreciated.

    Thanks in advance.


    EDIT:

    My htaccess file with @zessx proposed solution:

    Options -Indexes
    Options +FollowSymLinks
    
    DirectoryIndex index.php
    
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^my-website\.com$ [NC]
    RewriteRule ^(.*)$ http://www.my-website.com/$1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} !web1
    RewriteRule  ^(.+)$ http://www.my-new-website.com [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]