using mod_rewrite

using mod_rewrite

Posted by: vgjunkie
Posted on: 2006-03-17 12:21:00

I've been at this for a few hours now, and I can't seem to find anything that works. I've tried combinations of things and nothing seems to work out.

What I am trying to do is when a user visits:

http://domain.com/user

it will rewrite to:

http://domain.com/?u=user (or even /index.php?u=xyz)

"user" will be dynamic, so it can be user, person, 3xyzn ..etc

Now, I don't want it to rewrite if the file/directory actually exists. Any ideas on how to make this work?

I've tried this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^/(.*)$ /index.php?u=$1 [R,L]

It keeps giving me a 404. Any ideas?

Re: using mod_rewrite

Posted by: silkrooster
Posted on: 2006-03-17 16:50:00

Read about the get statement in the php manual.
You will find that a form even if it is a hidden input type will create a url with the ?name of varable=value in it. So a rewrite is not really required. But then again I am sure someone could think of a reason that I didn't.
The manual is at php.net.
Silk

Re: using mod_rewrite

Posted by: vgjunkie
Posted on: 2006-03-17 17:08:00

Not sure if I understand your response, or my initial question was not too clear.

Basically, if I typed in: http://domain.com/dir and it did not exist on the server, I would be given a 404 error. What I would like to do is to use mod_rewrite to take that and just redirect it to: /index.php?u=dir

So what I would like to happen is this:

- If the URI is not in the "file.ext" form (ala directory) and does not exist on the server, rewrite the url.

- If a directory/file *does* exist on the server, do not rewrite. (if I had a subdirectory called "gallery" and I typed http://site.com/gallery/ the rewrite would be called because I didn't specify /gallery/index.php)

- If a "file.ext" was requested and is not on the server, produce a 404 error (request http://domain.com/myfile.html and it does not exist, produce a 404 error)

I have updated my code a bit and have some success, but it still fails if the URI is not a "file.ext" format (e.g directory)

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ![a-z]+.[a-z]+
RewriteRule ^(.*)$ /index.php?u=$1 [L,NC]

Re: using mod_rewrite

Posted by: vgjunkie
Posted on: 2006-03-17 17:41:00

Got the answer for those who would be interested:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ![a-z]+.[a-z]+
RewriteRule ^(.*)$ /?var=$1 [L,NC]


The key was to use REQUEST_FILENAME and not REQUEST_URI when doing a check to see if file/directory exists.

Re: using mod_rewrite

Posted by: silkrooster
Posted on: 2006-03-17 23:04:00

Interesting, yep I misunderstood you. I thought you were trying to make a static page dynamic.
I got aways to go yet on learning rewrites.
Silk

Re: using mod_rewrite

Posted by: seiler
Posted on: 2006-03-18 09:22:00

Glad you got it worked out.

If you didn't find this while you were searching for your solution, it could come in handy in the future:

http://www.ilovejackdaniels.com/cheat-sheets/mod_rewrite-cheat-sheet/

Re: using mod_rewrite

Posted by: silkrooster
Posted on: 2006-03-18 20:46:00

Thank you for that cheat sheet, that should help a lot in learning rewrite rules. One question would you happen to know why when I use a rule to block images from referrers, that it blocks my site.
Here is the condition for my site:
RewriteCond %{HTTP_REFERER} !^http://(www.)?silkrooster.com/.*$ [NC]

BTW, I want to place this in a sub directory so only some images are blocked.
Silk

Re: using mod_rewrite

Posted by: silkrooster
Posted on: 2006-03-18 20:51:00

Just thought of something, I am using a firewall on my system, would that block the referer header? Only reason I ask is I know I need to disable the firewall in order for W3C's validater to work from a link.
Silk

Re: using mod_rewrite

Posted by: silkrooster
Posted on: 2006-03-18 20:57:00

Interesting, I got it to work. As long as I reaccess the page via url it works, just when doing a page refresh it does not work. The firewall does not effect it either btw.
Silk

Tags: request uridomainphpindexxyzcombinationsmodhttp