Friday, March 24, 2017
11:00:23 AM
Users online: 0   You are here >> Home > Web Design

Forums | Web Design Forums search
Forum FAQ
Apache mod_rewrite minus the infinite redirects
4/9/08 11:32:55 PM

Not sure if this is the right section, but here goes...

I'm looking to clean up the URL structure for a web site that's been around for a while. I don't want to break existing bookmarks and links to the site, so it's important that the current URL structure continues to work.

I want to turn something like this:
Into something like this:

I want to do a (not sure what the terminology is) force redirect from the former to the latter, so that if a user types /showproduct.cgi?etc it changes in their address bar to the nice new URL. However, the new URL has to transparently redirect back to the actual .cgi involved.

As I'm wanting to redirect from one to the other, there's a bit of a redirect loop happening that I'm not sure how to get rid of.

I've been using firefox to test my rewriting, which told me that it detected the site was redirecting in a way that would never complete.

I tried the [L] flag in my transparent rewrite rule, but that didn't seem to do the trick.

I'll post my actual rewrite rules on Monday, they're at work atm. But I was wondering if anyone can point me in the right direction as to how to stop rewriting at a particular point.

Thanks a lot!


4/9/08 11:50:35 PM
Sounds like you are actually after two rules, rewrite and redirect (two different ideas).

Rewrite will take site.com/products/12345 and parse it though to showproduct.cgi?itemno=12345

User will be able to use either link and the site would still work.

Maybe something like;

RewriteEngine on
RewriteCond $1 ^(products) [NC]
RewriteRule ^(.*)$ /showproduct.cgi?itemno=$1 [L]

Haven't tried it, but that will put you in the right direction on the rewrite part.

Redirect part. Let's see. I haven't used redirect in ages... I normally let PHP handle this with headers.

RedirectMatch /showproduct.cgi?itemno=(*)$ /product/$1

TOTALLY guessing here, but I hope it's a good guess for you.

Anything worth doing, might as well be done right.

8/9/08 11:51:21 AM

Ok, I'm still massively stuck. I'll simplify the example as I'm also having trouble with rewriting that involves query strings.

I want to redirect /viewlist.cgi to /cart

Then I want to rewrite /cart to /viewlist.cgi

I have the following:
RewriteEngine On
Redirect 301 viewlist.cgi /cart
RewriteRule ^cart$ /viewlist.cgi [L]

This loops infinitely. I believe that the [L] flag is terminating this iteration through my rewrite rules, but my ht.acl file is being read again after the rewrite to /viewlist.cgi.

I need some kind of conditional redirect/rewrite, but I can't figure out how to make RewriteCond work properly... it needs to know what the first request, not the current request, was for to be able to break the loop.

Basically, I need to check if the initial request is for /viewlist.cgi, and if it is, redirect to /cart. Otherwise no redirection should occur. However, it seems like %{REQUEST_URI} in a RewriteCond refers to the current URI, ie the redirected/rewritten one.

Surely there has to be a way around this! I've googled up some promising sounding results on webmasterworld.com, but it's blocked at work!


8/9/08 2:05:31 PM
will look into it.

Edited by rapidDazz: 8/9/2008 02:06:20 PM

Anything worth doing, might as well be done right.

8/9/08 9:43:06 PM

Have you tried using the [R] flag with RewriteRule, and no Redirect directive?

It's gonna be a glorious day!
I feel my luck could change.

9/9/08 7:34:18 PM
Hey mate,

I've tried and tried. Spent a few too many hours and searched far and wide - I'm almost convinced what you are trying to archive can not be done with .htaccess alone.

The way this normally works is you redirect /products/<something> to a particular file.php?itemno=<something> - and either redirect that back to the file name or simply leave it as a clean URL (the later being the more common and what you want).

The program you have written or are using, should be linking to the clean versions of your URL.

Anything worth doing, might as well be done right.

9/9/08 8:46:13 PM

Thanks for the help rapidDazz. I've been looking hard as well, and am ready to give up... it doesn't seem possible!

The reason it's such an obscure, annoying problem is to do with the software running behind the website that interfaces with the database. It's a lousy pile of crap, and I'm neck deep in forcing it to do things it was never meant to do... like having its dynamic content indexed by search engines! Who woulda thought you could write an online store that was so hard to buy things from... :S

Thanks again for the help, I really appreciate it :)

edit - oh yeah, I've updated the hard links to point to the new URL structure, but some (binary) CGIs automatically redirect to the old URLs and can't be changed.

Edited by stickmangumby: 9/9/2008 8:47:49 PM


9/9/08 11:56:08 PM
Quote by stickmangumby
Who woulda thought you could write an online store that was so hard to buy things from... :S

Never heard of OsCommerce? hehehe.

No worries mate, sorry I couldn't work it out :)

Anything worth doing, might as well be done right.

Forums | Web Design