0

I'm rewritting this

http://myweb.com/post/5/title-of-the-post (new url)

To

http://myweb.com/post.php?id=5 (old url)

I have achieved this by:

RewriteEngine On
#Options +FollowSymLinks
RewriteRule ^post/(.+)/(.+) post.php?id=$2

But if I try to enter to the old url it's still possible, so how can I redirect to the new one and not end up in a loop?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

1 Answers1

1

First, add this rule:

RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^post\.php$ post/%1/article [R=301,L]

This will force an immediate redirection and will not process any further rules. Requests to post/what/ever won't match this rule and skip on by. Note you have to create a generic article title, as .htaccess has no way of knowing the title to use.

Next, add this rule:

RewriteRule ^post/([0-9]+)/(.*)$ post.php?id=$1 [L]

Requests to post.php will have already been caught above, so they won't reach this line. This will do the redirection as expected and you should not have the endless loop.

Wige
  • 3,788
  • 8
  • 37
  • 58