Jump to content

regex mod rewrite help


kalyan02

Recommended Posts

Hello

 

I am stuck at converting

[good url which will be typed]

1) example.com/subject/?param=parameters

2) example.com/subject/

3) example.com/subject

 

i would like to mod-rewrite this to

[it will resolve into the below one at server]

1) example.com/index.php?slug=subject&param=parameters

2) example.com/index.php?slug=subject

3) example.com/index.php?slug=subject

 

any help would be appreciated

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/67305-regex-mod-rewrite-help/
Share on other sites

I'm not familiar with mod_rewrite, but I think it uses PCRE. Here's an example in PHP; you can try carrying over the pattern:

 

<pre>
<?php
$tests = array(
	'example.com/subject/?param=parameters',
	'example.com/subject/',
	'example.com/subject',
);
foreach ($tests as $test) {
	echo "<b>$test</b>:<br>";
	echo preg_replace('#(?<=/)(\w+)(/)?(?(2)\?(.+))?#', 'index.php?slug=$1&$3', $test);
	echo '<br>';
}
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

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