Jump to content

Complex htaccess rules


willo_the_wisp

Recommended Posts

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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 :-(

 

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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!

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.