htaccess Question

Discussion in 'HTML' started by Adrienne Boswell, Jun 20, 2009.

  1. I need to change folder/filename to folder_filename. This seems easy, but
    htaccess looks like Greek (or Chinese, or Armenian, or whatever languages
    you can't understand) to me.

    Thanks in advance.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
    Adrienne Boswell, Jun 20, 2009
    #1
    1. Advertising

  2. Adrienne Boswell wrote:

    > I need to change folder/filename to folder_filename. This seems
    > easy, but htaccess looks like Greek (or Chinese, or Armenian, or
    > whatever languages you can't understand) to me.


    Well, it _is_ tricky in the details (i.e. where the &Devil; lives).

    Part of the problem is that server software varies, and so do the settings,
    and a typical web server admin doesn't even bother telling the poor web
    authors what the settings are. So it's quite possible that even if you do
    things by the book, they won't work because the server admin disabled a few
    features. As far as I know, distributions of Apache and similar software
    have disabled more and more features by default. At the extreme, a web
    author cannot used .htaccess at all, except perhaps by paying some extra
    money to the ISP.

    You might think, after reading a nice tutorialish document like
    http://corz.org/serv/tricks/htaccess2.php
    that you could achieve your goal simply by adding

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)_(.*)$ $1/$2

    to your .htaccess file. (I'm assuming that "to change folder/filename to
    folder_filename" means "to make folder_filename work as an alias for
    folder/filename". If you really meant the opposite, you would just make the
    obvious modification.)

    However, the odds are that you get just some nasty 5xx error page, because
    the server settings won't let you use the rewrite engine.

    The next attempt is to use something like

    RedirectMatch permanent /~jkorpela/test/(.*)_(.*)
    http://www.cs.tut.fi/~jkorpela/test/$1/$2

    As you may guess, I tested this in my test directory only, as some of my
    URLs may contain "_", which might cause unpleasant surprises. I hope you can
    see how to modify the command. Note that the third argument must be a
    relative URL and the fourth argument must be an absolute URL! The rest is
    rather easy if you know the basics of regular expressions: .* matches any
    string, parentheses are essential (as only parenthesized expressions count
    in the replacement expression), and $1 and $2 indicate the strings that have
    matched the 1st and 2nd parenthetic expression.

    Beware that this is less efficient than the rewrite engine. This approach
    means that when a client requests for
    http://www.cs.tut.fi/~jkorpela/test/foo_bar
    then the server processes it and tells the client that the resource has been
    permanently moved to
    http://www.cs.tut.fi/~jkorpela/test/foo/bar
    and then the client requests for that. So there's a rather foolish HTTP
    transaction here. But this approach typically works if anything useful works
    in .htaccess according to your server admin settings.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 20, 2009
    #2
    1. Advertising

  3. Gazing into my crystal ball I observed "Jukka K. Korpela"
    <> writing in
    news:%mb%l.15676$:

    > Adrienne Boswell wrote:
    >
    >> I need to change folder/filename to folder_filename. This seems
    >> easy, but htaccess looks like Greek (or Chinese, or Armenian, or
    >> whatever languages you can't understand) to me.

    >
    > Well, it _is_ tricky in the details (i.e. where the &Devil; lives).
    >
    > Part of the problem is that server software varies, and so do the
    > settings, and a typical web server admin doesn't even bother telling
    > the poor web authors what the settings are. So it's quite possible
    > that even if you do things by the book, they won't work because the
    > server admin disabled a few features. As far as I know, distributions
    > of Apache and similar software have disabled more and more features by
    > default. At the extreme, a web author cannot used .htaccess at all,
    > except perhaps by paying some extra money to the ISP.
    >
    > You might think, after reading a nice tutorialish document like
    > http://corz.org/serv/tricks/htaccess2.php
    > that you could achieve your goal simply by adding
    >
    > Options +FollowSymlinks
    > RewriteEngine on
    > RewriteRule ^(.*)_(.*)$ $1/$2
    >
    > to your .htaccess file. (I'm assuming that "to change folder/filename
    > to folder_filename" means "to make folder_filename work as an alias
    > for folder/filename". If you really meant the opposite, you would just
    > make the obvious modification.)
    >
    > However, the odds are that you get just some nasty 5xx error page,
    > because the server settings won't let you use the rewrite engine.
    >
    > The next attempt is to use something like
    >
    > RedirectMatch permanent /~jkorpela/test/(.*)_(.*)
    > http://www.cs.tut.fi/~jkorpela/test/$1/$2
    >
    > As you may guess, I tested this in my test directory only, as some of
    > my URLs may contain "_", which might cause unpleasant surprises. I
    > hope you can see how to modify the command. Note that the third
    > argument must be a relative URL and the fourth argument must be an
    > absolute URL! The rest is rather easy if you know the basics of
    > regular expressions: .* matches any string, parentheses are essential
    > (as only parenthesized expressions count in the replacement
    > expression), and $1 and $2 indicate the strings that have matched the
    > 1st and 2nd parenthetic expression.
    >
    > Beware that this is less efficient than the rewrite engine. This
    > approach means that when a client requests for
    > http://www.cs.tut.fi/~jkorpela/test/foo_bar
    > then the server processes it and tells the client that the resource
    > has been permanently moved to
    > http://www.cs.tut.fi/~jkorpela/test/foo/bar
    > and then the client requests for that. So there's a rather foolish
    > HTTP transaction here. But this approach typically works if anything
    > useful works in .htaccess according to your server admin settings.
    >


    You are my hero, Jukka! Now, when you explain it, it makes more sense.
    Yes, I do want to do a permanent redirect.

    I'm going to have to play around with it until I get it working correctly
    (went somewhere strange once, got a 500 once). I'll keep at it.

    Right now I'm using a custom 404, and it seems to be working okay. But,
    I'll keep at until I get it working.

    Thanks again!

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
    Adrienne Boswell, Jun 20, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    Charset - htaccess question

    Samuël van Laere, Jul 9, 2003, in forum: HTML
    Replies:
    9
    Views:
    3,539
    T. Audry Glamour
    Jul 9, 2003
  2. Bart van den Burg

    .htaccess question

    Bart van den Burg, Sep 15, 2003, in forum: HTML
    Replies:
    3
    Views:
    454
    Bart van den Burg
    Sep 16, 2003
  3. mentalguy2004

    htaccess question...

    mentalguy2004, Aug 24, 2004, in forum: HTML
    Replies:
    3
    Views:
    393
    lostinspace
    Aug 24, 2004
  4. nllamas

    .htaccess - htm / html question

    nllamas, Apr 1, 2006, in forum: HTML
    Replies:
    3
    Views:
    532
    nllamas
    Apr 1, 2006
  5. BusyGuy

    .htaccess question please

    BusyGuy, Sep 21, 2006, in forum: HTML
    Replies:
    6
    Views:
    489
    Jukka K. Korpela
    Sep 23, 2006
Loading...

Share This Page