I have the following PHP code:
<?php
if(isset($_GET['article']))
{
echo "<b>success</b>";
}
?>
And this html:
<a href="http://localhost/PHPTest/index.php?article_27" >Click me</a>
I am trying to come up with my own url.
By doing so, I try to use mod_rewrite(). This is what is in my .htaccess:
RewriteEngine On
RewriteBase /PHPTest/
RewriteRule ^article_([0-9]+)$ index.php?article=$1
As you can see, I try to get the article get variable..
This however doesnt work.. The htaccess does not comprehend artcile_27, for example, as article=27..
Why is that?
What should happen is that the success word should be printed whenever I press the link. The problem is that it does not.
Your rule should be:
However, I’d question the use of mod_rewrite in this way.
First of all, if your url is always just
index.php?article_27then you can get directly at “article_27” using$_SERVER['QUERY_STRING'], without mod_rewrite at all.If you’re going to use mod_rewrite, why not have your URL be
/PHPTest/article/27?The above will also allow
/PHPTest/article/27/title_of_article_here, which gives you two nice things: the ID is still there so it’s easy to retrieve server-side, and the name is in the URL so it’s nice to read as a human and helps search engines. If you look in your URL bar now, you’ll notice stackoverflow itself uses this method.