mod-rewritequery-stringfriendly-urluriencoding

mod_rewrite for pretty url with uriencoded query strings


ok, so I've tried many things I've found on SO and elsewhere, but I just can't get it to work, always receiving a 404 error code.

I'd like to enter this url:

memorizeit.com/pics/220.0.8251.20120905002352.7982368227/Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter.+Getting+grass+stains.

and have it invisibly convert to:

memorizeit.com/pics/index.php?pic=220.0.8251.20120905002352.7982368227&title=Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter

The index.php page is looking for:

$pic = ($_GET["pic"]);
$title = ($_GET["title"]);

In my .htaccess file in the /pics directory I've got the following:

RewriteEngine on
Header set Cache-Control "max-age=2592000"
RewriteRule ^pics/([^\/]*)/([^\/]*)/([^\/]*)$ /pics/index.php?pic=$1&title=$2&extra=$3 [L,QSA]
RewriteRule ^pics/(.+)/(.+)$ /pics/index.php?pic=$1&title=$2 [L,QSA]
RewriteRule ^pics/([^\/]*)$ /pics/index.php?pic=$1 [L,QSA]

I've tried it without the QSA, I've tried it with either .+ (anything) and [^/]* (anything except /) I left both in so you can see how I've put them there (I think!). I do plan on making the $1 allowed to only include numbers and periods, but I'd just like to get it to work being wide open first.

I can't figure out why it isn't working. In my base url .htaccess file I have:

RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

And it always redirects to https so I know the .htaccess files are being read. I don't know what else to try. Any thoughts would be greatly appreciated. Thanks!


Solution

  • Turns out that because pics/ is a real directory the rewrite was trying harder to get there than to just go ahead and rewrite it. So I changed the pics/ to p/ in the matching statement and moved the rules to the base .htaccess file and it works as expected.

    Wow that took a long painful time... Hope it saves someone else some time.