user3177012
user3177012

Reputation: 681

Rewrite URL with htaccess file

I've searched through countless blogs and looked at stack overflow questions about this and I still just don't understand it.

What I have is a blog website with the url as:

http://website.local.co.uk/article.php?id=123-123

But I want to use mod rewrite to change it to:

http://website.local.co.uk/article/123-123

I'd also like to add a title to this to become:

http://website.local.co.uk/article/123-123/the-title-goes-here

Currently I am using:

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

But this keeps redirecting me back to error 404 page as it's not picking up the ID number. Any ideas how to achieve what I want it to do?

Upvotes: 0

Views: 111

Answers (2)

anubhava
anubhava

Reputation: 784888

put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^article/([0-9]+-[0-9]+) /article.php?id=$1 [L,QSA,NC]

Upvotes: 0

ablshd
ablshd

Reputation: 15

Normally this can be done effectively using a router. Look up PHP Router on github, there are some useful router classes there. Also, routers usually come as part of a MVC framework such as CakePHP or Laravel. You can even use a micro-framework such as Slim or Flight.

Upvotes: 1

Related Questions