I’m working on a photo gallery that uses “Friendly” URLs, and so far everything is working quite well.
I have a Rewrite rule that makes this:
/gallery/index.cfm?album=album&photo=44
Look like this:
/gallery/album/subalbum/44
No you don’t! The redirection is going the other way!
My problem now is that some albums are pretty huge, so I’d like to add pagination. I wrote something to append ?page=1 to the end of the URL string, but that seems to “break” my setup.
That’s the function of the QSA flag - retain the original (page=whatever) query string.
Here are the rewrite rules I have now. Keep in mind that some albums have sub-albums (/album/sub-album/pic), and some just have pics (/album/pic)
RewriteRule gallery/(\\w+)/$ /gallery/index.cfm?album=$1 [NC]
RewriteRule gallery/(\\w+)$ /gallery/index.cfm?album=$1 [NC]
RewriteRule gallery/(\\w+)/(\\d+)$ /gallery/index.cfm?album=$1&photo=$2 [NC]
RewriteRule gallery/(\\w+)/(\\d+)/$ /gallery/index.cfm?album=$1&photo=$2 [NC]
RewriteRule gallery/(\\w+)/(\\w+)$ /gallery/index.cfm?album=$2 [NC]
RewriteRule gallery/(\\w+)/(\\w+)/$ /gallery/index.cfm?album=$2 [NC]
RewriteRule gallery/(\\w+)/(\\w+)/(\\d+)$ /gallery/index.cfm?album=$2&photo=$3 [NC]
RewriteRule gallery/(\\w+)/(\\w+)/(\\d+)/$ /gallery/index.cfm?album=$2&photo=$3 [NC]
[indent][SIZE=4][COLOR=“#FF0000”]WARNING: You should NEVER follow a filename with a / as that changes the directory level of the request which will result in support files not being found (assuming you use relative links).
Using the No Case flag in a RewriteRule will only cause you problems, too! NC is designed to be used on variables which are not case sensitive (URIs ARE case sensitive) like {HTTP_HOST}.[/COLOR][/SIZE]
Then, I’m not very enamored with your (correct) use of the word metacharacter (\w; I prefer [a-zA-Z]) but, if you understand regex, don’t change it! Ditto the digit metacharacter (\d vs [0-9]).
With those comments, try:
RewriteEngine on
RewriteRule gallery/(\\w+)$ gallery/index.cfm?album=$1 [QSA,L]
RewriteRule gallery/(\\w+)/(\\d+)$ gallery/index.cfm?album=$1&photo=$2 [QSA,L]
RewriteRule gallery/(\\w+)/(\\w+)$ gallery/index.cfm?album=$2 [QSA,L]
RewriteRule gallery/(\\w+)/(\\w+)/(\\d+)$ gallery/index.cfm?album=$2&photo=$3 [QSA,L]
I’ve also removed the leading / in the redirection as that forces Apache to look to the server root before your DocumentRoot.[/indent]
This might look horrific to some of you, but I am a COMPLETE novice when it comes to Mod Rewrite.
You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.
I’m just not sure how to add an argument for &page=$ to the URL without breaking what’s there now.