htaccess Question

A

Adrienne Boswell

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.
 
J

Jukka K. Korpela

Adrienne said:
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.
 
A

Adrienne Boswell

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top