1
votes

I am having a problem with rewritting the URL using .htaccess, When the URL is something that looks like that original url: http://www.example.com/page.php?id=1&name=test after rewrite: http://www.example.com/page-1-test it works fine

but the manager asked for hyphens instead of spaces so i did this

$name = str_replace(" ", "-", $name);

then i echo $page_name http://www.example.com/page.php?id=2&name=test-some-other-text

so it would look like this http://www.example.com/page-2-test-some-other-text

in the it takes 2-test-some-other-text as the id when i try to $_GET['id'];

and here is my rewrite rule in the .htaccess

RewriteRule page-(.*)-(.*)$ page.php?id=$1&name=$2
1
Try RewriteRule page-(\d+)-(.*)$ page.php?id=$1&name=$2 this is better control of groups. - Jones
page-(.*) will capture anything after page-, it won't stop at the other dash. - Evan Mulawski
@Jones Your solution also worked so great thanks for your help all of you - Mohamed Hassan

1 Answers

3
votes

Since the ID is a digit, try this rule:

RewriteRule page-([0-9]+)-(.*)$ page.php?id=$1&name=$2

It will only match digits for the ID and then end at the final dash instead of keeping on trying to greedy match.