0
votes
  • I have an website on my localserver (with Apace) and i want to rewrite urls.
  • I have .htaccess file.

  • I have RewriteEngine ON (on Apache)

index.php

<a href="post.php?post=<?php echo $row['postID'];?>"><h4><?php echo $row['postTitle']; ?></h4></a>

When someone press on the title of post will be redirected to post.php/post=postID

I want to rewrite this url (localhost/post.php/post=postID) to something like that:

/localhost/post/postTitle or /localhost/post/postID

Preferably the first one.

How can I do that?

.htaccess

RewriteEngine On
RewriteRule    ^posts/([0-9]+)/?$    post.php?post=$1    [NC,L]

Nothing happens, when I press on the link, the url is the same (post.php/post=postID).

1
Well yeah, the rewrite rule rewrites e.g. /posts/45 to post.php?post=45, not the other way around. So you probably want to update your a href link to use the new URL structure. - Jonnix

1 Answers

0
votes

It is a manual process to update your links. So, you'll want to change:

<a href="post.php?post=<?php echo $row['postID'];?>">
    <h4><?php echo $row['postTitle']; ?></h4>
</a>

to:

<a href="posts/<?php echo $row['postID'];?>">
    <h4><?php echo $row['postTitle']; ?></h4>
</a>

Update: As a note, people like to use the shorthand for just echo'ing content, E.g.

<?=$row['postID']?>

So it would become:

<a href="posts/<?=$row['postID']?>">
    <h4><?=$row['postTitle']?></h4>
</a>