Jump to content


mod_rewrite ... please help ... I'm lost

  • Please log in to reply
3 replies to this topic

#1 sammydafish

  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNeed deep in mod_rewrite hell

Posted 18 November 2003 - 08:27 PM

I\'m putting up a CMS system that I just finnished. I want to use mod_rewrite to handle get variables to my main script. I\'ve tried a few examples, but I can\'t seem to get this to work properly.

What I\'d like to do is have:
be displayed as

I used this in my httpd.conf file:

RewriteEngine on
RewriteRule /([a-z]+) /index.php?page=$1

and I was able to get it working for a little bit. I put in in the <directory> directive. I tried to put it in the .htaccess file, but it did nothing when I did. Basicaly, I have a few questions. One, how come it doesn\'t seem to work when I put it in the .htaccess file. Two, how can I write a rule that will check to see if the request actualy exists in the file system, if so, serve the actual file, not rewrite it.
Example: within the document root, there is a path \'inventory\' so
when www.mydomain.com/inventory is requested, I want to serve up the default document in the directory, not index.php?inventory. How?

Is there a good document that explains all the things used to write a rewrite rule? I\'m confused by the use of carrots and parens and brackets and what not. I read teh apache documentation, but it was not easy to understand :roll:, hence, I\'m here asking.

I read the article: http://www.phpfreaks...orials/23/0.php It wasn\'t really much help.

Also, what\'s the best place to put rewrite rules, and what <directory > options or permissions need to be on in order to use it?


lost :arrow: - Anthony

#2 sammydafish

  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNeed deep in mod_rewrite hell

Posted 18 November 2003 - 08:35 PM

BTW, forgot to mention, Apache 2.0.45 on Win2K Thanks :)

#3 Dissonance

  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationChicago, IL

Posted 18 November 2003 - 11:11 PM

For your inventory link, you wouldn\'t even need mod_rewrite. If /inventory/ is a top-level directory in your document root, you can just navigate to www.yoursite.com/inventory/ and Apache will load the index file automatically.

You just need to tweak your code a bit.

RewriteEngine On# Will send anyone asking for www.yoursite.com/whatever# (with or without trailing slash and regardless of case) to index.php?page=whatever# AS LONG AS \'whatever\' is not \'inventory\'. In theory, anyway.# I haven\'t tested this out yet.RewriteRule ^/([^inventory]+)/?$ index.php?page=$1 [NC]

To learn how to use mod_rewrite, you should look up regular expressions. Once you get that down, the little brackats and carots will all make sense.
Friends don't let friends vote Republican.

#4 sammydafish

  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNeed deep in mod_rewrite hell

Posted 19 November 2003 - 02:33 AM

Thanks man.

This is what ended up workin for me:

RewriteEngine On
RewriteRule ^(inventory/?.*) - [L]
RewriteRule ^([^.]+)$ index.php?page=$1 [L]

There might be a better way to do it, but after much trial and error, this is working.

The 1st one just checks for the inventory possability, and sends the user there. The other will run the script. This rule will also let me link to other files in the file system as long as I call them with extentions. i.e
someotherfile.php will work. not rewrite to index.php?page=someotherfile.php Took me a while to figure out how to do that. The thing that helped me the most was simple to know ot look for info on regular expression. I found htis document http://www.amk.ca/python/howto/regex/ that helped me a LOT!

One thing I couldn\'t figure out, what\'s the [L] or [NC] mean?

Thanks for your help!! :lol:

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users