willo_the_wisp Posted February 10, 2012 Share Posted February 10, 2012 Hi all, I've just taken control of a site (written by another developer), which has a load of unfriendly URLs that the client would like me to make 'friendy'. I've done this using .htaccess and mod-rewrite I need to re-write URLs such as the following, but don't know how to write the rules to do so. An example of the type of URL I have to convert is as follows: /09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 Needs to become: /frames/aluminium/1 I need to pattern match &0=1 and $search=1 as these are variable and there are hundreds of variations. &o will become the number at the end of the friendly URL. &search can be discarded. I thought I'd need to do something like this in .htaccess: Redirect 301 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 /frames/aluminium/ However I can't make this work at all. Can anyone please help? Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/ Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 okay here's what I've come up with for you. Since you haven't given me a lot to work with as far as what values can possibly in the values spots of the querystring, i will go off of specifically what you gave me. RewriteEngine On RewriteRule ^/([a-z]+)/([a-z]+)/([0-9]+)$ /09search_results.php?search_KEYWORD=$1&search_RANGE=$2&o=$3 [NC, L] Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1316656 Share on other sites More sharing options...
willo_the_wisp Posted February 11, 2012 Author Share Posted February 11, 2012 Hi AyKay47, Thanks for your reply, much appreciated. Looking at your solution, I was full of hope that it would work... but it doesn't unfortunately. I didn't give you many values because there are literally thousands of combinations, hence the reason I need a Rewrite rule to take care of them. search_KEYWORD can be 'aluminium','black','silver','birch', and lots of other values. search_RANGE can be 'Frames','Prints','Photos' and lots of others. $o is a parameter for the database offset, and can be anything from 1 - 1000+ $search I think can be 0 or 1 but it wouldn't surprise me if it could be 0 - 9 given the way the previous developer has put this site together. I tried your rule in my .htaccess file and it had no effect at all. /09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 stubbornly refuses to be translated to /frames/aluminium/1 Here's what I have in my .htaccess file at the moment: Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC] RewriteRule ^(.*)$ http://www.sitename.co.uk/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http://www.sitename.co.uk/ [R=301,L] ### RewriteRule Pattern Substitution [OptionalFlags] RewriteRule ^ready-made-frames/?$ ready-made-frames.php [L] # Ready-made frames page RewriteRule ^frames/([^/\.]+)/?$ frames.php?search=$1 [L] # Ready-made frames category page RewriteRule ^frames/([^/\.]+)/([^/\.]+)/?$ frame-details.php?search=$2 [L] RewriteRule ^frames-by-size/?$ frames-by-size.php [L] # View frames by size page #RewriteRule frames/([^/\.]+)/([0-9]*)$ frames.php?search=$1&page=$2 #Ready-made frames + pagination #RewriteRule ^/09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 /frames/aluminium/ [L,R=301] RewriteRule ^/([a-z]+)/([a-z]+)/([0-9]+)$ /09search_results.php?search_KEYWORD=$1&search_RANGE=$2&o=$3 [NC, L] #ErrorDocument 400 /400.php #ErrorDocument 401 /401.php #ErrorDocument 403 /403.php #ErrorDocument 404 /404.php #ErrorDocument 500 /500.php Everything's working apart form the 09search_results.php rule. Any more ideas? Thanks, Willo Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1316988 Share on other sites More sharing options...
AyKay47 Posted February 11, 2012 Share Posted February 11, 2012 I wrote the rewrite backwards, change to: RewriteEngine On RewriteRule ^/09search_results.php?search_KEYWORD=([a-z]+)&search_RANGE=([a-z]+)&o=([0-9]+)&search=[0-9]+$ /$2/$1/$3 [NC, L] Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317035 Share on other sites More sharing options...
willo_the_wisp Posted February 13, 2012 Author Share Posted February 13, 2012 Hi AyKay47, Yes I thought you'd written it backwards the first time, but I tried swapping it around myself but it didn't work. Unfortunately your last solution above doesn't work either! I'm really scratching my head with this one. My other rules work, but not this one!! Any more advice you can offer would be appreciated! Willo Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317507 Share on other sites More sharing options...
requinix Posted February 13, 2012 Share Posted February 13, 2012 RewriteRule doesn't include the query string when it matches URLs. RewriteEngine On RewriteCond %{QUERY_STRING} ^search_KEYWORD=([a-z]+)&search_RANGE=([a-z]+)&o=([0-9]+)&search=[0-9]+$ [NC] RewriteRule ^/09search_results.php$ /%2/%1/%3 [NC, L] Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317509 Share on other sites More sharing options...
willo_the_wisp Posted February 13, 2012 Author Share Posted February 13, 2012 Hi Requinix, Unfortunately that doesn't work either... it causes a 500 error. Kelly Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317513 Share on other sites More sharing options...
AyKay47 Posted February 13, 2012 Share Posted February 13, 2012 looking back at your original post, the solution I provided here: okay here's what I've come up with for you. Since you haven't given me a lot to work with as far as what values can possibly in the values spots of the querystring, i will go off of specifically what you gave me. RewriteEngine On RewriteRule ^/([a-z]+)/([a-z]+)/([0-9]+)$ /09search_results.php?search_KEYWORD=$1&search_RANGE=$2&o=$3 [NC, L] is what you want. If someone types in /frames/aluminium/1 they will be served: /09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 which is what you asked for. It is up to you to modify your code to cater this rewrite, e.g changing the internal links etc.. Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317595 Share on other sites More sharing options...
willo_the_wisp Posted February 13, 2012 Author Share Posted February 13, 2012 Hi AyKay47, I think you've misunderstood me. 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1 is the OLD URL, which has been cached by Google, and therein lies the problem. Google will send traffic to 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1 for the next few months, until Google eventually removes it from its index. Therefore if Google sends someone to that URL, I need it to redirect the page to /frames/aluminium/1 I've already got a rule to redirect /frames/aluminium/1 to frames.php, which is working. But I can't get any .htaccess rules (including yours) to work to do the first part. Willo Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317608 Share on other sites More sharing options...
AyKay47 Posted February 13, 2012 Share Posted February 13, 2012 My thoughts are to redirect to the pretty url, then have the rewrite serve the correct file until Google indexes the proper url. RewriteEngine On RewriteCond %{HTTP_REFERER} .*google.*$ RewriteCond %{THE_REQUEST} ^[a-z]+\ 09search_results\.php\?search_KEYWORD=([a-z]+)&search_RANGE=([a-z]+)&o=(\d+)$ [NC] RewriteRule ^09search_results\.php$ %2/%1/%3 [R=301,NC,L] RewriteRule ^([a-z]+)/([a-z]+)/(\d+)$ 09search_results.php?search_KEYWORD=$2&search_RANGE=$1&o=$3 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1317665 Share on other sites More sharing options...
willo_the_wisp Posted February 17, 2012 Author Share Posted February 17, 2012 Hi, I'm still bashing my head against a wall with this. Almost nothing works! My client really needs this to be up and running but I just can't make it work. So, to re-iterate, in my .htaccess file, I have the following: Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC] RewriteRule ^(.*)$ http://www.sitename.co.uk/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http://www.sitename.co.uk/ [R=301,L] ### RewriteRule Pattern Substitution [OptionalFlags] RewriteRule ^ready-made-frames/?$ ready-made-frames.php [L] # Ready-made frames page # Works fine RewriteRule ^ready-made-frames/([^/\.]+)/?$ frames.php?search=$1 [L] # Ready-made frames category page # Works fine RewriteRule ^ready-made-frames/([^/\.]+)/([^/\.]+)/?$ frame-details.php?search=$2 [L] # Works fine RewriteRule ^frames-by-size/?$ frames-by-size.php [L] # View frames by size page # Works fine RewriteRule ^09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1+$ /ready-made-frames/aluminium/ [R=301,NC,L] #NOTE: This is the simplest example of this rewrite I could find, but it just doesn't work. Google has indexed loads of URLs like the one in the last RewriteRule above. If Google (or another search engine), sends a request to 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1, what I need it to do is replace the URL in the browser address bar with /ready-made-frames/aluminium/ Simple, you'd think? But I just can't make it work. If I change the last rewrite rule to RewriteRule ^09search_results.php /ready-made-frames/aluminium/ [R=301,NC,L] it works fine. But as soon as I put in any of the querystring, it stops working. The URL in the address bar stays resolutely at 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1, rather than being replaced. Please please if anyone can help, I'm getting really frustrated with this. Thanks to AyKay47 and Requinix for suggesting solutions before, but they didn't work either :-( Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1318389 Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 I have overlooked the fact (not sure how) that rewrite rules ignore query strings unless specified in a condition, as requinix pointed out. Really, his code should work for you, you will want to add a 301 redirect for google indexing: RewriteEngine On RewriteCond %{REQUEST_URI} ^/09search_results\.php RewriteCond %{QUERY_STRING} ^search_KEYWORD=([a-z]+)&search_RANGE=([a-z]+)&o=([0-9]+)&search=[0-9]+$ [NC] RewriteRule ^/09search_results.php$ /%2/%1/%3 [R=301,NC, L] Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1318409 Share on other sites More sharing options...
willo_the_wisp Posted February 24, 2012 Author Share Posted February 24, 2012 Hi, Just to let you know, with your help above and a couple of other sources, I finally cracked it. To save anyone with the same trouble the hours of head-scratching that I've had, here's the solution: #Transform this search-engine-indexed URL: 09search_results.php?search_KEYWORD=aluminium&search_RANGE=Frames&o=1&search=1 #INTO this: /ready-made-frames/some-var/ RewriteCond %{REQUEST_URI} ^/09search_results\.php?$ #Check that the request is for this specific file RewriteCond %{QUERY_STRING} ^search_KEYWORD=([a-z]+)&search_RANGE=([a-z]+)&o=([0-9]+)&search=[0-9]+$ [NC] #Check that the querystring follows this specific format RewriteRule ^(.*)$ /ready-made-frames/%1/? [R=301,L] #Rewrite it into 'friendly URL' format #Variables %2, %3, and %4 are redundant in this new URL structure Thanks for your help AyKay47 and Requinix. We got there in the end! Quote Link to comment https://forums.phpfreaks.com/topic/256826-complex-htaccess-rules/#findComment-1320786 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.