testing mod_rewrite
garydr
Status: Curious
Joined: 20 Jul 2007
Posts: 5
Reply Quote
I'm trying to learn to use RewriteCond and RewriteRule but i have no experience with regex. Is there a tool somewhere that one can use to test the effect of a trial RewriteCond / RewriteRule expression?

Thanks
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
Not that I know of. Here's the absolute, no excuses minimum requirement for starting to play with mod rewrite:

Set up a real apache server, ideally on linux box.

Learn how it works.

Learn how to set up a real virtual site.

Then learn how, within that site definition section, to implement mod rewrite rules. Mod rewrite is VERY hard to learn to do reasonably well.

If you can't do this, then set up a testing DIRECTORY inside a currently existing site and use .htaccess files to to test your rules.

It is imperitave, at least while you're learning, to have a working test environment.

Study existing samples of mod rewrite. and start reading about regular expressions.

At some point you have to learn the basics of regex, so there's almost no point in trying to deal with something as complicated as apache mod rewrite while having no idea of regex at all, it's just too hard.

In my opinion, mod rewrite on apache was the single most difficult thing I've learned with web programming stuff, so don't pretend to yourself that there is some quick and easy cheat route, you will just end up making really serious mistakes at some point because you'll be implementing stuff you don't understand. There are few syntaxes more terse than apache mod rewrite, and few methods where it's easier to make fatal loop conditions that will take down the server instantly.

So read on it.

There might be some cheat forms or something, but I can virtually guarantee you that you're better off either having someone teach you your specific requirement, then explain it to you, or stopping and spending some time learning the basics.

mod rewrite is an amazing product, very powerful, and lets you do virtually anything you want to do, it's really very impressive, but because it has so much power, one thing it lets you do is crash the page/server instantly with one tiny mistake.

So you really must have a test server setup for local development as a starter, anything else is just a waste of time long term.

See our apache forums for more tips and how tos.

Do not ask someone to write your stuff for you however, on forums, it's hard to do this stuff, and you shouldn't expect people to do your work for your for free, it's not polite.
Back to top
garydr
Status: Curious
Joined: 20 Jul 2007
Posts: 5
Reply Quote
I was afraid that would be the answer. i have what appears to be a very simple requirement, so i hoped i could quickly learn enough to do it. my one attempt went badly wrong so i quickly backed out of that.

i guess i'll have to look for help.

thanks for responding.
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
If you have a simple one time requirement, yes, you are better off looking for help, it's not in my opinion worth the time required to learn the basics if all you need is a line or two of .htaccess code.

If it's utterly trivial post your requirement here and I'll take a look, if it requires any real time I won't suggest any code.
Back to top
garydr
Status: Curious
Joined: 20 Jul 2007
Posts: 5
Reply Quote
Since I know so little, in my ignorance it seems like it should be very easy. Thanks for offering to look at it.

"www.botanicalpaperworks.com/dep_product.php?par_catalog_id=nn"

should become

"www.botanicalpaperworks.com/dep_product_new.php?"par_catalog_id=nn"

In other words, I need dep_product.php to become dep_product_new.php.
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
that would be in the 'trivial' department.

This assumes that mod rewrite is in fact functioning and enabled on the Apache web server.

:: Code ::
RewriteEngine On
RewriteRule ^dep_product.php dep_product_new.php [R=301]


This assumes some things. One, you want the page renaming to be permanent, which is what the R=301 says. If it was not permanent, it would say: R=302

Two, it assumes that you may need other rules after that rule. Otherwise, if that's the only one, you'd add: [R=301,L]
The L stands for Last rule to be applied.

Three, all the internal site links are pointing to the new page url, not the old one. This is a common oversight.

Note, this is all saved to root directory of site, as .htaccess file

If .htaccess support is not enabled on the web server, there's nothing I can do about that, it's a server setting.

.htaccess files must end with a new line/ line break, in other words, the last line must be blank or you may get a server error.

If you upload this file and the site gives a 500 error, immediately comment out with # in front of the two above lines and reload the file.

If there is already an .htaccess file, use that and simply add these lines.

the . in front of the file is part of the file name, it's not optional.

Do not overwrite an existing .htaccess file, use the one that exists if it does exist or you will cause massive disasters.

If the server guy runs rewrite stuff already internally using httpd.conf instead of .htaccess, you should use that. But I doubt that's the case.

However, note that the rewrite syntax is slighty different when it runs on httpd.conf file, which is the actual apache config file. Usually using .htaccess is easier.

Remember, when uploading changes, test instantly, and if there are any problems, immediately comment out the rewrite rule above, then reload it and try to figure out what went wrong.
Back to top
garydr
Status: Curious
Joined: 20 Jul 2007
Posts: 5
Reply Quote
First let me answer some of your assumptions.

1. Yes, this is to be permanent.
2. I have a .htaccess file and there is already a block of mod_rewrite Directives.

I tried your suggestion but what it generated was:
"http://www.botanicalpaperworks.com/home/bpw2004/public_html/dep_product_new.php?par_catalog_id=50"

when what I want is

"http://www.botanicalpaperworks.com/dep_product_new.php?par_catalog_id=50"

At one time i paid someone to create a set of directives that would change my URL's into search engine friendly ones. The directives are in pairs - an example follows:
:: Code ::
RewriteCond %{REQUEST_URI} ^/productX/
RewriteRule ^productX/.*/([0-9]+)/? dep_product_newX.php?par_catalog_id=$1


Is something else required in what you suggested that would stop the full pathname of the file from being a part of the generated URL?

Again, thanks for your assistance,
Gary
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
If you have competing rules you'll probably need to again pay someone to update your stuff, the odds of error are too high, and conflicts are too easy to get into, as are loops and other problems.
Back to top
garydr
Status: Curious
Joined: 20 Jul 2007
Posts: 5
Reply Quote
OK, well thanks for your willingness to help.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours