htaccess 301 redirect

Discussion in 'Apache' started by gprerna, May 1, 2012.

  1. #1
    Hi,

    I am a newbie..plzz help me out in this.

    The scenario is like this:

    There is a folder on the root named as "application". Inside this folder there are subfolders containing index.php files.

    So, when we tried to access the subfolders containing index.php files , the url on the address bar will be

    www.somewebsite.com/application/folder1/index.php

    Now, we dont want "application" to be displayed in the url.

    To fulfill the above requirement,

    In .htaccess we added a code snippet :

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/application/.*$
    RewriteRule ^folder1/(.*)$ /application/folder1/$1

    This successfully solves the problem by making the url access the page in both ways
    URL1 : www.somewebsite.com/application/folder1/index.php
    URL2 : www.somewebsite.com/folder1/index.php

    But , now the problem is :

    I want to do 301 redirect whenever URL1 is clicked , it should redirect to URL2.

    How to achieve this in .htaccess ? can you provide me with some generic code..

    Any help is appreciated...
     
    gprerna, May 1, 2012 IP
  2. nikhilrajr

    nikhilrajr Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    This is the correct .htaccess code for 301 redirection

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/application/.*$
    RewriteRule ^folder1/(.*)$ /application/folder1/$1 [R=301,L]
     
    Last edited: May 10, 2012
    nikhilrajr, May 10, 2012 IP
  3. gprerna

    gprerna Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you nikhilrajr, The above codespec when added to htaccess it redirects me from www.somewebsite.com/folder1/index.php to www.somewebsite.com/application/folder1/index.php. I want it to redirect in a reverse way. Plz have a look on the above scenario for better understanding.
     
    gprerna, May 10, 2012 IP
  4. nikhilrajr

    nikhilrajr Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/application/.*$
    RewriteRule ^folder1/(.*)$ /application/folder1/$1 [R=301,L]
     
    nikhilrajr, May 14, 2012 IP
  5. nikhilrajr

    nikhilrajr Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this also

    RewriteEngine On
    RewriteRule ^folder1/index.php$ application/folder1/index.php [NC,L]
     
    nikhilrajr, May 14, 2012 IP
  6. phd

    phd Active Member

    Messages:
    496
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Try RedirectMatch also
     
    phd, May 14, 2012 IP