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
https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/
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.

 

 

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.