Enjoy unlimited access to all forum features for FREE! Optional upgrade available for extra perks.

Any .htaccess rewrite gurus

Status
Not open for further replies.
Joined
May 31, 2007
Posts
2,126
Reaction score
187
I'm wanting to do a conditional rewrite with differing numbers of parameters some optional.

Is this possible

Code:
www.domain.co.uk/search/butcher/town/mytown
www.domain.co.uk/search/butcher/county/mycounty/page7
www.domain.co.uk/show/123

this would need to go into index.php as

Code:
index.php?function=search&category=butcher&town=mytown
index.php?function=search&category=butcher&county=mycounty&page=7
index.php?function=show&key=123

The page value is optional
 
Last edited:
I can't think of an easy way but this is way into RegEx not so much htaccess.

You would need your index to use a switch statement on the function element, I expect from there the htaccess wouldn't be too hard.

I'll have another look after I get back home, I'm about to head out in 30 minutes or so.

If your familiar with basic rewrites once you break up the function switches the rest should be simple.
 
I'm interested too

Basically a way to pass 'x' parameters based on number of 'x' folders

Looked into this previously but couldn't find an answer

I ended up having to hardcode all alternative number of parameters

/1/2/3/ -> page.php?parameter1=1&parameter2=2&parameter3=3
/1/2/ -> page.php?parameter1=1&parameter2=2
/1/ -> page.php?parameter1=1

I'll have a look this as would be really useful :)
 
Maybe something like

RewriteRule ^/(.*)/?(.*)/town/?(.*) index.php?function=$1&category=$2&town=$3

I haven't checked this but it may give you a starting point.
 
Would need to pass a blank variable for optionals like page= then use the PHP to disregard the option, with an if $_post['page'] == "" then $page=0 kinda deal.
 
This looks best I've found

Code:
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 ^.*$ test.php?%1=%2 [QSA,L]


MOD REWRITE Guide & Common Requests - Dev Shed

GL
 
Nick, yours is easy

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*) sample.php?var1=$1&var2=$2&var3=$3

That will call the sample.php document and populate var1-3 with the folder names.

Try it with

<?php
echo "1 = " . $_GET['var1'] . "<br>";
echo "2 = " . $_GET['var2'] . "<br>";
echo "3 = " . $_GET['var3'] . "<br>";
?>

in the sample.php file.

The other one is complex due to not using the same function, you could write mod_rw for each function but it would be a killer regex to do it in one go.
 
Last edited:
Thanks for the replies

Using

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*) sample.php?var1=$1&var2=$2&var3=$3

could this cope with variable number of parameters or would I need a rule for each number of parameters? Handling in the PHP code is quite simple as the function would define the number of parameters I'm expecting and my code alread reads the input and sanitises it before assigning to local variable / SQL query string.
 
I guess as long as your URLs was contructed the same way everytime...

You would just need to sort it out in php.

You would need to pass it ALL the possible variables, so if there was 7 possibles you would need to pass it all 7 every time, then use php to test the content of the variable.

Because, Town, Country, Search, Show all seem to be static variables, you could write 1 rule for each 'type' and simply check for the type.

case 'search':
$var1 = blah
etc

case 'show':
etc

Then you could disregard the exess fragments there.

The important bit is that your URL structure never changes otherwise the variables would too.
 
I was thinking more of the regex rather than the PHP, I'll know from function what expect, but the URL will have differing number of parameters and my limited regex knowledge would suggest that sample above is working with just 3 parameters and would fail if just 2.

I guess I'll need to devote an hour or so to "fiddle time" and see what I can make it do.
 
My RegEx is limited too, the one member I introduced to the forum who was very very good at regex was chased away and stoned like a martyre and refuses to come on the forum now.

The multiple rules allow you to determin which is it.

RewriteEngine on
RewriteRule ^search/(.*)/town/(.*)/(.*) sample.php?function=search&type=$1&town=$2&page=$3
RewriteRule ^search/(.*)/county/(.*)/(.*) sample.php?function=search&type=$1&county=$2&page=$3

Then in PHP test page and set to 0/1 if not set.

Write a rule for each one, it the 'easiest' option, I expect more advanced regex as possible but they are beyond my basic understanding of it.

Something like that may work.
 
Why not use the simple way like:

ErrorDocument 404 /index.php

and then do the rest of coding in php? In this case variable is passed, and you can easily add extra redirections if needed. This is the way some url shortening services work, when giving <domain>/<short> redirection.
 
I'm on my not so smart phone so this may come out as more crap than usual.

If you rely on your error handler to do this, you wouldn't realistically and reliably be able to determine what was an error, what was a legit request and what was someone trying to inject.

Aside from the best practice and good form issues.

You would need php to parse and split the current path to convert it into chunks then push the pieces into variables then operate on them to determine search or show etc, then produce your page. I think i got that right its hard to tell when you can only see 50 to 70 characters at a time :)

A busy site would eat a shared resource server alive first in processor time then in memory.

Least thats if i understood your meaning. :)
 
I'm on my not so smart phone so this may come out as more crap than usual.

If you rely on your error handler to do this, you wouldn't realistically and reliably be able to determine what was an error, what was a legit request and what was someone trying to inject.

Aside from the best practice and good form issues.

You would need php to parse and split the current path to convert it into chunks then push the pieces into variables then operate on them to determine search or show etc, then produce your page. I think i got that right its hard to tell when you can only see 50 to 70 characters at a time :)

A busy site would eat a shared resource server alive first in processor time then in memory.

Least thats if i understood your meaning. :)

It depends on how you handle the error. You don’t need to parse (as far as I can see form the task) but simply query the path (as a single variable) against database or, if not many redirects are needed, against text file. The redirect is also stored in the database or text. So we are talking about single command, if FALSE is returned, send to 404. Not really resource-hungry in this way. Meanwhile for many redirects or possible redirects you will get quite hefty .htaccess and this will slow the system down.

P.S. If you need just 1-2 redirects, you can even "hardcode" them into index.php so no database is needed.
 
Last edited:
I can't see how you could not parse it when the function would accept search, view, show or whatever else so you would need to split out the function, the search function then accepts either county or town, maybe even city so you'd need to work that out. On top of this you have an optional variable of page to check.

I guess you could do like url shortening scripts do, the last one i worked on basically took the first x characters after the / ran a query then returned based on those characters but with 1000s of towns/city/counties it would call ache.

I think i'm misunderstanding what your idea is. I'm sure if you've done it, it works but i can't visualise the code or methods and not near an interpreter to try it.

My suggestion is half a dozen rewrite rules that matches each distinct set. 1 for county, 1 for city, 1 for town, 1 for view etc, with 1 test and set for page.

I'm sure you could use the or operator in regex to town||city||county etc to reduce the lines too.
 
I can't see how you could not parse it when the function would accept search, view, show or whatever else so you would need to split out the function, the search function then accepts either county or town, maybe even city so you'd need to work that out. On top of this you have an optional variable of page to check.

I guess you could do like url shortening scripts do, the last one i worked on basically took the first x characters after the / ran a query then returned based on those characters but with 1000s of towns/city/counties it would call ache.

I think i'm misunderstanding what your idea is. I'm sure if you've done it, it works but i can't visualise the code or methods and not near an interpreter to try it.

My suggestion is half a dozen rewrite rules that matches each distinct set. 1 for county, 1 for city, 1 for town, 1 for view etc, with 1 test and set for page.

I'm sure you could use the or operator in regex to town||city||county etc to reduce the lines too.

It's just fine with rewrites, not a problem. I think we probably talking about different tasks. I suggested simple handler like this (not a complete code but gives an idea):

function line_redirect($line) {
global $dbcheck;

$qry = "SELECT * FROM $dbcheck WHERE id = '$line';";
$out = mysql_query($qry) or die ("Error: Query failed");

$rows = mysql_num_rows($out);
if (($rows != 0) && (mysql_result($out, 0, "url") != "")) {


<put whatever you need here, like redirects>


}

<real 404 here>

Maybe we are talking about different redirect types?
 
Last edited:
Status
Not open for further replies.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Featured Services

Sedo - it.com Premiums

Sponsors

IT.com

Premium Members

AucDom
UKBackorder
Be a Squirrel

Sponsors

Acorn Domains Merch
MariaBuy Marketplace

Shiny Nuts

Perfect
Service
Laskos
URL Shortener
*the exceptional businesses of our esteemed moderators
Top Bottom