views:

40

answers:

1

EDIT @mootinator has done awesome work with this. See his answer below.

Since I wasn't clear in specifying my exact URI, it may still help people who are looking for something similar. My URI is more like: 9/Here-is-some-text/unwatch

...in which case you need mod_rewrite rules that look a little more like this:

RewriteEngine on
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

Pretty minor modification to @mootinator's answer, but took me a few minutes to figure out on my own. May help someone save time on down the line.


I'm trying to do a mod_rewrite to pass in a GET parameter, a sort of boolean flag so that whenever someone goes to URI/unwatch, you can pull out &unwatch=1.

I've got this rule already:

RewriteRule ^(\d+)/* index.php?id=$1 [L]

I would like to have another rule that does something like this:

RewriteRule ^(\d+)/unwatch index.php?id=$1&unwatch=1 [L]

The "directory" ./unwatch is not a directory at all. The directory structure looks like:

./index.php
./.htaccess

Any thoughts on how to do this?

+2  A: 
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]

QSA stands for Query string append. This way parameters passed by the user don't get erased.

If there's something I'm missing involving getting around the url not being an actual directory, check out how CakePHP does it.

Working demo here using the following .htaccess:

<IfModule mod_rewrite.c>
   RewriteBase /mod_rewrite
   RewriteEngine on
   RewriteRule ^([0-9]+)/?$ index.php?id=$1 [QSA,L]
   RewriteRule ^([0-9]+)/unwatch/?$ index.php?id=$1&unwatch=1 [QSA,L]
</IfModule>

Turns out the space I had in [QSA, L] was causing the 500 error, and \d wasn't behaving as I expected.

mootinator
I'm getting an Internal Server Error with that rule for some reason. Looked at the Cake link but having trouble grepping it.
Josh Smith
My spidey sense thinks the / in the search pattern needs to be escaped. But it would be wrong.
mootinator
Is there a better way to do this that involve creating a directory but maybe modifying the rewrite rule above somehow?
Josh Smith
@mootinator Unfortunately still getting 500 error. Wondering if maybe my ordering of the rules is wrong or I need to modify the initial rule somehow?
Josh Smith
I'm thinking /? at the end of the url is more sane than /* which would match any number of slashes, but that shouldn't make a difference.
mootinator
Could be that the second rule is unreachable. The first rule will always match the second pattern unless you add the $ to denote the end of the url.
mootinator
This is really starting to bug me. I appreciate the help so far, but still getting a 500 no matter what I do. I'm sitting here doing variations on http in d minor. It's pathetic. Any further ideas?
Josh Smith
Oh, and if it really is easier to do everything differently (using a different dir) I'm up for it. This is fast becoming a waste of my night.
Josh Smith
My advice: Check the error log, wait for someone more familiar with mod_rewrite to answer, or ask your question on http://webmasters.stackexchange.com/
mootinator
@mootinator Thanks. I've since moved on to more interesting tasks. Hopefully I can figure this out at some point. And thanks for your help so far.
Josh Smith
I have an example running now.
mootinator
Awesome, awesome work. I'm going to edit my post to say what I actually did, since my URI is slightly different from what you have (my fault for that). This is so great, though. Really, thanks.
Josh Smith
And seeing this in action now is also amazing. I wish I could show everyone here just how fantastic this is. Too bad you'd have to sign up and we're just in pre-alpha. :[
Josh Smith