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

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.