Reputation: 30252
What rule translates urls like so?
host.com/plastic_toys/pink_barbie/
host.com/index.php?q=plastic+toys+pink+barbie
The number of components in the category and product name vary.
Edit:
I guess this rule does part of the job:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?q=$1+$2 [L]
The problem with the above rule is that it does not take care of converting underscores to +
, so it translates like this:
host.com/plastic_toys/pink_barbie/
host.com/index.php?q=plastic_toys+pink_barbie
I guess I have to delegate that conversion to PHP. Now is there a way to test if the rule actually does the conversion the way I imagine it would?
Upvotes: 1
Views: 130
Reputation: 1449
First - I would suggest using the following rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)/?$ /index.php?q1=$1&q2=$2 [L,QSA]
The RewriteCond each check that a valid file or directory does not exist (for instance if you want to have a static /help.html and it exists, or /FAQ/index.php and it exists then the RewriteRule will not fire.
I've split out the parameters into 2 items - so you can treat them as separate parameters (if you want to treat it as 1 parameter just change it to /index.php?q=$1-$2 or some other separator that you want - I would say do not use + since you want to parse off that character anyway).
Upvotes: 1
Reputation: 2941
Is there a reason (existing code, etc) that you need to do this totally as a rewrite?
Normally, I do
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
and then parse $_GET["q"]
or even just parse $_SERVER["REQUEST_URI"]
directly. preg_match()
is your friend here.
It takes way less time to use one of these two methods than it does trying to come up with a weird rewrite.
Upvotes: 3
Reputation: 606
Nothing. How would a rule know where to draw the line between a category and product. What if the query had 3 parameters. Are the first two the category and the last the product or is the first the category and the last two the product.
Upvotes: 0