Andy-H Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/ Share on other sites More sharing options...
premiso Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/#findComment-1278984 Share on other sites More sharing options...
Andy-H Posted October 13, 2011 Author Share Posted October 13, 2011 cheers, managed to get it done, had to do several rules like RewriteRule (.*)[^/] ?page=$1 [NC,L] RewriteRule (.*)[^/]/(.*)[^/]/(.*)[^/] ?page=$1&$2=$3 [NC,L] //etc... Quote Link to comment https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/#findComment-1279088 Share on other sites More sharing options...
requinix Posted October 13, 2011 Share Posted October 13, 2011 There's this from a forum I frequent. Should be exactly what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/#findComment-1279160 Share on other sites More sharing options...
Andy-H Posted October 14, 2011 Author Share Posted October 14, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249018-need-help-with-rewrite-for-query-string/#findComment-1279268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.