Jump to content

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


sammydafish

Recommended Posts

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:

www.mydomain.com/index.php?page=products

be displayed as

www.mydomain.com/products

 

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.com/tutorials/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?

 

Thanks

 

lost :arrow: - Anthony

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.