Hello, I'm new to mod_rewriting in general and I have a question about a multi-variable rule. I posted this in general programming because I'm not sure if it needs some PHP (or any server side language) code in addition to a mod_rewrite rule. Mods, feel free to move my topic if it's not in the right spot. RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)/(.*)/(.*)$ index.php?p=$1&a=$2&id=$3 [QSA,L] Code (markup): After a bit of googling I modified a script I found and it works great when there's 3 variables but it 404s when there's only 1 or 2 variables. The non-rewrite page would look something like: index.php?p=clients&a=edit&id=5 So, as you can see the ability for there to NOT be 3 variables and load a specific page for that is required. If someone just went to clients then it would list all of the clients. There might be only 1 variable, 2, or 3 and I need to load a specific page for each one. Best example would be a blog if my example isn't clear... Someone might go to: www.website.com/2010/07/17/cool-seo-title-page but also go to: www.website.com/2010/07/17 (to list today's blog entries) or: www.website.com/2010/07 (everything in july) or: www.website.com/2010 (everything in 2010) Basically, how can I write a rule to handle multiple variables where the variables might not be present in the URL?
No one has any idea? Surely there has to be someone who has something setup like this. I read the sticky in the Apache forms and his method does not work (it fails) with Apache 2.2.x.
Since there aren't that many combinations, you could just set up individual RewriteRules. Something like: RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p=$3&a=$2&id=$1 [NC,L] RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p=preset&a=$2&id=$1 [NC,L] RewriteRule ^([^/]+)/?$ index.php?p=preset&a=preset&id=$1 [NC,L] Code (markup): Where 'preset' is the default value or just miss it out and assign it a value in the PHP script.
Why are you using ([^/]+) instead of (.*) to filter? I'm not sure of the difference. I ended up getting it to work with: RewriteRule ^(.*)/(.*)/(.*)$ index.php?p=$1&a=$2&id=$3 [L] RewriteRule ^(.*)/(.*)$ index.php?p=$1&a=$2 [L] RewriteRule ^(.*)$ index.php?p=$1 [QSA,L] Code (markup): It's weird though. In the base directory of the site I don't need the QSA flag for the single variable rewrite rule but if I go down 1 directory then I need the QSA flag or the link does not work. Any idea why this happens?
This matches any character that's no a forward slash, with the + quantifier (so one or more). QSA maintains the original query string, not sure why it's working the way it is without seeing the URL structure.
The structure is: index.php contains the main page code and will include a specific file based on "p" (page). "a" handles an action (new, edit, view, etc.). "id" is the id of the item (I'll be changing this to the title/subject/etc. on pages where the title is unique). All of the included pages are in the same directory as the index page, and I also have a sub-directory called "/dev/". The index.php file in the /dev/ folder works the same way as the main page index file. It successfully works in the root directory without QSA but fails in the /dev/ folder unless I put in QSA. When it fails it just loads up the home page, it doesn't throw an error. It loads up the home page because in my PHP code I run a switch on "p" to determine which page to load, and in all cases where "p" is not something I specifically inserted a case for it falls back to the home page.
A new problem I came across was handling the same number of variables but the variable names are different, it kind of relates to this topic. I think once this is figured out it this thread alone has enough to SEO'alize anything they could possibly want. Example: Let's say you have an admin page that handles your site's users. If you load p=users (/users/) it would list every user. If you load p=users&i=joe (/users/joe/) it would list a page where you can edit joe's details. If you load p=users&n=3 (/users/3/) it would load page 3 for your user list, assume you have pagination. If you load p=users&a=subscribed (/users/subscribed/) it would list users who are subscribed to your service or whatever. So here we have a situation where we need to rewrite 3 urls where all 3 of them have only 2 variables but the variables are different. How the heck do you rig this up? Doing the following just doesn't work: RewriteRule ^(.*)/(.*)$ index.php?p=$1&i=$2 [L] RewriteRule ^(.*)/(.*)$ index.php?p=$1&a=$2 [L] RewriteRule ^(.*)/(.*)$ index.php?p=$1&n=$2 [L] Code (markup):