redirect all html files in root except index.html

Discussion in 'Apache' started by billens, Jan 1, 2008.

  1. #1
    I'm trying to redirect all .html files in my root directory to a new directory with the exception of index.html

    I moved all html files in the root to a new directory with the name /s/
    except the index.html

    I don't want to redirect files in subfolders.

    I can't find out how to resolve this problem with .htaccess.
    I tried many things but created only internal server errors.
     
    billens, Jan 1, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ index.thtml
    RewriteRule ^([^/]+)\.html$ s/$1.html
    RewriteRule ^index\.thtml$ index.html
    Code (markup):
     
    joebert, Jan 1, 2008 IP
  3. billens

    billens Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you joebert

    Doesn't this redirect also the .html files in the subfolders?
     
    billens, Jan 2, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    I've attached a zipfile that contains a directory named "rewrite".
    Inside rewrite there is
    1. Directory "s"
    2. Directory "a"
    3. .htaccess
    4. index.html
    5. test.html

    Inside both "/rewrite/s" and "/rewrite/a" are an "index.html" and "test.html".

    Each of the *.html files contain their path from the rewrite directory, so "/a/test.html" contains the text "a/test.html".

    If you place rewrite in the sites root, so you have http://site.com/rewrite/ then you should see the following.

    site.com/rewrite/index.html -- "index.html"
    site.com/rewrite/test.html -- "s/test.html"

    site.com/rewrite/s/ -- "s/index.html"
    site.com/rewrite/s/test.html -- "s/test.html"

    site.com/rewrite/a/ -- "a/index.html"
    site.com/rewrite/a/test.html -- "a/test.html"

    If this was to be in the site root instead of contained within the rewrite directory, then you would change the value of "RewriteBase" in ".htaccess" to "/" instead of "/rewrite".

    In case you're wondering, I'm using Apache2-Debian.
    <Directory /var/www/>
    	Options Indexes FollowSymLinks MultiViews
    </Directory>
    Code (markup):
     

    Attached Files:

    joebert, Jan 3, 2008 IP
  5. billens

    billens Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes!

    Thank you very much!

    Would it be possible to change as well the directory in the url
    if a file triggers "RewriteRule ^([^/]+)\.html$ s/$1.html"

    p.e.
    if I put in the address bar: http://site.com/rewrite/test.html
    this file will be loaden:
    http://site.com/rewrite/s/test.html
    but in the address bar is still this url:
    http://site.com/rewrite/test.html

    Is it possible to let the users see the new url?
     
    billens, Jan 4, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    The RewriteRule can have a redirect flag set to let the browser know it should use the new address instead.

    It's as simple as adding [R=301] to the end of the RewriteRule
    RewriteRule ^([^/]+)\.html$ s/$1.html [R=301]
    Code (markup):
    This results in the request for "/test.html" returning a 301 status and telling the browser to go to "/s/test.html" from now on instead.


    With the way it was in my previous example, the browser never knew the swap existed. Basicly doing a "you say tomAto I say tomato".
     
    joebert, Jan 4, 2008 IP
  7. billens

    billens Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Something went wrong.
    I received an error.
     
    billens, Jan 5, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    What exactly did you do ?
     
    joebert, Jan 5, 2008 IP
  9. billens

    billens Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I added [R=301] to:
    RewriteRule ^([^/]+)\.html$ s/$1.html

    New rule:
    RewriteRule ^([^/]+)\.html$ s/$1.html [R=301]

    However my domain is a virtual domain and when I add [R=301]
    the real domain is showing up in the url bar with an error:
    The requested URL /rewrite/s/test.html was not found on this server.
     
    billens, Jan 8, 2008 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    In that case, you'll need to use the entire url in the RewriteRule with the 301 attached.
    RewriteRule ^([^/]+)\.html$ http://domain.com/s/$1.html [R=301]
    Code (markup):
     
    joebert, Jan 8, 2008 IP
  11. billens

    billens Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    That's it!
    Thank you very much.
    You're an expert :)
     
    billens, Jan 8, 2008 IP