Thursday, 12 November 2015 11:19

    htaccess 301 with folder or file exception

    Written by
    Rate this item
    (0 votes)
    Web Development Web Development Bello group ltd

    I like to develop my websites in a live environment which saves time when it comes to switching it to the world. It also provides an excellent platform for error checks and live behaviour. In order to be able to develop online and on my servers without having to work on my local host, I create a directory which I name say "dev" and which I exclude from search engine crawling in my robot.text file.

    I however came across a challenge with one of my domain which has a 301 redirect. How could I access my newly created folder since when trying to access it I would be redirected. Here is the exception rule and code which allowed me to overcome the challenge:

    RewriteCond %{REQUEST_URI}!^/my-folder/

    RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

     

    The above code needs to be added to your .htaccess file. The code permanently redirects with a 301 redirect all traffic to the site to http://www.newdomain.com, except requests to resources in the  /my-folder directories.  The user are redirected to the equivalent of the page they requested by using the (.*) capture group and then including $1 in the new URL.

    Read 6625 times Last modified on Friday, 13 November 2015 18:39
    Fabien Bello

    Fabien Bello has been working in web and app develoment since 1997. He has held numerous positions from game developer to head of creative and product development for major firms such as Xerox, Gala Coral Group. Fabien continues contributing to online community, owns and manages several websites as well as offering consultancy to all size businesses.

    Leave a comment

    Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.