Htaccess and php preg_match?

Hey,

I’m just wondering how to use the preg_match function in php to meet the variables of the htaccess

strictly to the point, hope you understand my silly english (:

look in htaccess we use things like

RewriteRule ^([^/]+)/$ ./test.php?url=$1

what i’m looking for is what is the variable which we use in the preg_match function to meet the code

([^/]+)/

and how to add many variables like

RewriteRule ^([^/]+)/p([0-9]+).html$ ./test.php?url=$1&page=$2

what is the equivalent of these variables in the preg_match function ?

cuz i’ve seen alot of the examples like this one :eek:

preg_match("/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i", $string);

but i didn’t understand what’s in it. :confused:

well, the PHP preg_match looks like it checks if it’s a valid email address.

Are you asking how to pass matches into a variable in php?

preg_match($pattern, $string, $match);
//if there's a match, it will be passed into $match variable as an array

the htaccess examples you gave above is more of a preg_replace function in php where $n (n is any number). here’s an example from http://www.php.net/manual/en/function.preg-replace.php


$string = 'April 15, 2003';
$pattern = '/(\\w+) (\\d+), (\\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);

//output: April1,2003

each $n variable is based on each pattern/subpattern

yes i know that the example i wrote above was for checking if an e-mail is valid

and i know how to use the preg_match function but my question is what are the patterns that’s equivalent to the htaccess codes which i can use in the preg_match function ??

To have PHP pick up those variables from the previous mod_rewrite you showed us, you would simply do this in your test.php

test.php


<?php

echo 'page is ' . $_GET['page'];
echo 'url is ' . $_GET['url'];

well thank you but i already know that, actually every php beginner knows that :smiley:

my question is what is the pattern that’s equivalent to this code “([^/]+)”, this code “([^/]+)/p([0-9]+).html”, and so on.

i just want to redirect all the links to the test.php file which in turn match the requested link to the exact pattern i want then send data which i got from the link pattern to another page. like in vbseo plugin all links are redirected to the vbseo.php file.

Are you saying you want PHP to redirect the user depending on the value of $_GET[‘url’], and the pattern you are looking to match is something like p123.html ?

If that is not what you mean, then try and spend a bit more time and attention in giving us an actual specific example of what you do want, and maybe someone will be able to help you out.

guess i’ve pissed you off and i’m sorry didn’t mean to

i’m not depending on the value of $_GET[‘url’]

i’m depending on the value of $_SERVER[‘REQUEST_URI’]

guess now there’s no mystery in my question :wink:

Im not pissed off, but I am getting exasperated because I fail to understand your problem. Dont worry, a forum is sometimes not the best method for getting ideas across – time lags, language differences etc.

Let’s try this:

Give us a concrete example of a value that $_SERVER[‘REQUEST_URI’] would hold (just make one up that typifies your problem).

Then explain what the regex has to extract from that - don’t describe it in terms, show us exactly the text or decision that has to be extracted from that $_SERVER[‘REQUEST_URI’]

([^/]+)/

Is a valid preg_match pattern, which reads “One or more of anything that isnt a /, followed by a /”.

How to decode it:
() is a subpattern. the / on the end is a literal /. So we’ve dealt with those. Remaining pattern: [^/]+
is a character class. + after a character class means “One or more of the preceding class”.
^ at the beginning of a character class means “negate the class”
the / is again a literal /.
So now that you’ve broken it down, build it back up:
^/ “Not a slash.”
[^/]+ “One or more not-a-slash-es”
([^/]+)/ “One or more not-a-slashes followed by a slash.”, which, reworded, is “One or more of anything that isnt a /, followed by a /”.

http://us.php.net/manual/en/reference.pcre.pattern.syntax.php

EDIT: Note that the ^ has another meaning if used outside of a character class.

ok then, I’m working on a cms script and one of the urls (the topic one) should be like

http://www.example.com/cmsdir/category/topic1/topic-name.html

when i redirect the urls to the test.php file which includes the following simple code

<?php

echo $_SERVER['REQEST_URI'];

?>

it prints me out

/cmsdir/category/topic1/topic-name.html

and the htaccess file has the following code


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ test.php [L,QSA]

i want to use the preg_match function to match the requested url to an exact pattern that is

/category/topic1/topic-name.html

and if it matches this pattern the test.php file sends the variables (that i extract from the url such as $category,$id and $name) to another file which has the query codes

and thank you for your patience :slight_smile:

So… the request URI contains /cmsdir/category/topic1/topic-name.html, and you want to capture category, topic1, and topic-name.

preg_match(“~/cmsdir/([^/]+)/([^/]+)/([^/]+).html~”,$_SERVER[‘REQEST_URI’],$matches);

$matches[1] = category
$matches[2] = topic1
$matches[3] = topic-name

well i tried the code ([^/]+)/ in the test file and it get’s me the next error

Warning: preg_match() [function.preg-match]: Unknown modifier '/' in C:\\xampp\\htdocs\\cmsdir\	est.php on line 3

and here’s the content of the test.php file


<?php

Preg_Match('([^/]+)/', $_SERVER['REQUEST_URI'], $Match);

if(isset($Match[1])) {
echo "Good";
}else {
echo "Bad";
}

?>

well let me try this one :slight_smile:

Great One, this is my pattern :smiley:
when i copied the code it gave me an error then i realized that REQUEST_URI is missing the U ;), anyway it’s working :smiley:
many thanks <3 <3

another simple question

when i use code like

preg_match("~/news/?~",$_SERVER['REQUEST_URI'],$matches);

when i use the link like “http://www.example.com/cmsdir/news/” it matches

and if i use it like “http://www.examples.com/cmsdir/news/anything-here” it matches too :S

is there anyway to match only the “http://www.example.com/cmsdir/news/” link ??

“~/news/?$~”

You are Awesome :smiley: thank you <3