Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Replacement for .htaccess?


MrPhil

Recommended Posts

I am trying to get a website (my own PHP code + canned applications such as osC) to do some Stupid Pet Tricks. I have discovered why .htaccess programming is referred to as "voodoo" even in its own documentation -- it's horribly structured, inconsistent, and works only half the time. Anyway, I'm quite frustrated with .htaccess and seek an alternative for an Apache-based site.

 

The chief sticking point with .htaccess has been SEF URIs (I want /path/module/key1/val1/key2/val2 to become /path/module.php?key1=val1&key2=val2). This is easy enough for a fixed number of parameters, but none of the suggestions I've tried for generalized (variable number of parameters) work. .htaccess only goes through one pass, leaving me with /path/module/key2/val2?key1=val1 or some other wreckage like that. Maybe it's a limitation of my host, maybe I just haven't found the right incantations yet.

 

I was able to write a nice "redirector" PHP script that uses normal PHP code and $_SERVER variables to pick apart the URL and put it back together the way I want, and then use header("Location: xxxx") to go to the revised URL and status code. It's called from /.htaccess once per page. It works beautifully, except when POST data is involved. The only way I've found to deal with that is to set up a <form> with hidden data fields, and automatically submit it (as POST). It's a bit ugly (the form flashes up for a second or two), but works. The last problem, which I have been unable to solve, is that something like a CAPTCHA doesn't work. Apparently, the "correct" answer changes between the first and second page calls, so it's never a match. Has anyone gotten around this? I want to avoid altering canned software if at all possible, otherwise I would serialize the POST data and pass it through a $_SESSION variable, or something. 403 and 404 error handling is a bit of a kludge, but that's secondary.

 

So, can anyone offer pointers on either 1) foolproof cookbook ways to set up .htaccess URI rewriting to handle SEF variable length false paths, 2) foolproof ways to pass POST data through a single PHP page, or 3) some other .htaccess replacement altogether? I know in something like WordPress, if a request is not a real file or directory, that it will pass it to index.php and let PHP code play with it and internally redirect to the right place. However, I'm try to avoid making changes to canned software, and would like it to think it's running under normal circumstances.

 

Is there an open source replacement for .htaccess, with procedural language instead of rewrite rules? .htaccess is associated with Apache -- how do Nginx and various Windows servers handle these tasks? Do they just emulate .htaccess? I'm on a shared server, so it can't be something that the host needs to install (I'm sure they won't). Thanks much for any leads!

Link to comment
Share on other sites

Yeah, I can see that they're using somewhat-like .htaccess rewriting rules, each for a fixed number of parameters. I know how to do it for a fixed number of parameters, and it's easy within a PHP "redirect" script, but I'd really like to have a general case that could handle any number of parameters in a false path, and convert them to a URL Query String. I suppose that worst case, I could add N rules for N parameter key=val pairs to the .htaccess files, but I'm hoping there's a better way. If it weren't for the issue of POST data, and especially things like CAPTCHAs, I'd be all set with my current PHP redirector script.

 

.htaccess rewriting probably started out simple, and just got out of hand over time, as people tried to squeeze more and more function out of it. It really could use a procedural language to handle things much more cleanly. What would be superb would be for Apache to let me supply a URL rewriting script (and set status code) of my choice, to skip the .htaccess mess.

Link to comment
Share on other sites

I don't know...why not just /path/module/val1/val2? It's your own framework sounds like.../path/module would be the route and anything after would be the get vars...just a thought...

 

 

The chief sticking point with .htaccess has been SEF URIs (I want /path/module/key1/val1/key2/val2 to become /path/module.php?key1=val1&key2=val2).

Link to comment
Share on other sites

Tim @@clustersolutions, I'm not sure what you're proposing. The real path is /path/module.php, which wants GET variables key1=val1 and key2=val2. While links can certainly be set up that way, people have come to expect clean-looking SEF style links of the form /path/module/key1/val1/key2/val2. What I'm looking for is a way to change arbitrarily long key/val chains into URL Query Strings. It's trivial in PHP code, but .htaccess is a pain.

 

Is the only way to do this is for a maximum of N key/val pairs, to set up N rewrite rules? I was hoping to avoid that, but .htaccess looping constructs just don't work for me. On top of that, .htaccess is so flaky that when I fix one thing, another breaks. Grrr!

Link to comment
Share on other sites

  • 1 month later...

I seem to have gotten the SEF path-to-Query-String working for arbitrarily long key/value lists (fingers crossed). Now I've spent the last several weeks trying to figure out why my 301 redirect to add www. to the domain doesn't work if the URI starts with a real directory (e.g., /dir4). It works only if the URI starts with a fake (virtual) directory that later gets a 200 silent rewrite to a real full path (Actually, if the /dirN is omitted in the original URL, /.htaccess inserts a default /dirN. The rest of the path is real. If I explicitly give a /dirN in order to request a different code branch, /.htaccess fails to do the www redirect.). I'm not sure whether all the .htaccess files are being skipped down to the final directory of the page being displayed, or they're being processed, but some things (like URL changes) don't get inherited down the line. Neither way seems to be how I understand .htaccess should work.
 
I got this reply from my hosting "support" on my ticket asking why the redirect didn't work:
 
 

Please note that your are using custom .htaccess files for your sub-folders as following:

/home/****/public_html/dir4/.htaccess

So, http://catskilltech.com/dir4 is not redirecting to http://www.catskilltech.com/dir4 . By definition, .htaccess applies to the resident directory and all sub-directories. If you use a custom .htaccess file for a sub-folder, then that .htaccess file becomes parent .htacess file for that folder and cannot inherit the redirection to www . So, you need to add a new www. redirect for taht specific folder where you use custom .htaccess.

 
Does anyone understand this? It sounds to me like they're saying that the root .htaccess was skipped. Am I misunderstanding how .htaccess and URL rewriting are supposed to work?
 
If this is a reasonable thing on the part of my host, I think I can make it work by using a fake root directory like /d4, and at the end of /.htaccess, rewrite (200) it to /dir4. However, I'd like to avoid such games if I can.

Link to comment
Share on other sites

The saga concludes. I finally was able to prove to my host that when a URI starts with a real directory, it skips over all the higher level .htaccess files (especially /.htaccess) and goes directly to the desired directory. That, to my understanding, is not correct behavior (it's supposed to process all the .htaccess files down the directory chain). I have a workaround of using virtual (fake) directory names to start a URI, that /.htaccess rewrites to real directory names. It's ugly, but at least the site works now.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...