Jump to content

Need help with rewrite for query string


Andy-H

Recommended Posts

I have google'd a little but can't find the solution for this.

 

 

Basically I want to rewrite url's like

 

 

/products/category/1

 

 

to

 

 

?page=products&category=1

 

 

I have the .htaccess for the page sorted

 

 


<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) control.php?page=$1 [NC,L]
</IfModule>

 

 

And could quite easily update it to fit the above rule, however, I was wondering if there is a way to make it work at any level i.e.

 

 

/products/category/1/product/3/action/purchase

 

 

Would result in

 

 

?page=products&category=1&product=3&action=purchase

 

 

BUt without doing a

 

 

(.*)/(.*)/(.*)/ ?page=$1&$2=$3 etc.

Link to comment
Share on other sites

RewriteRule ^products/.*/(.*)/action/(.*)$ something.php?page=products&category=$1&action=$2 [L]
RewriteRule ^products/.*/(.*)$ something.php?page=products&category=$1 [L]

 

Since you know products is static (not sure if by category you mean the actual category or just the word) you use that as your anchor. The rest is pretty self explanatory. Trying to do it all dynamically would be just a pain in the butt and quite unnecessary. This should get you what you are after.

 

 

Link to comment
Share on other sites

There's this from a forum I frequent. Should be exactly what you're looking for.

 

 

Perfect, thanks :)

 

 

Managed to adapt it to my needs

 

 


<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)/([^/]+)/([^/]+)$ $1?$2=$3 [QSA,E=TEST:$1]


RewriteCond %{ENV:TEST} ^(.+)/([^/]+)/([^/]+)$
RewriteRule ^.*$ %1?%2=%3 [QSA,E=TEST:%1,N]


RewriteCond %{ENV:TEST} ^([^/]+)$ [OR]
RewriteCond %{REQUEST_URI} ^([^/+])/(.+)$
RewriteRule ^.*$ control.php?page=%1&%2 [QSA,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ control.php?page=$1 [NC,L]
</IfModule>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.