1

Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.

I have an empty .htaccess file I have a current URL of http://www.website.co.uk/news.php?id=111111 I want this to become http://www.website.co.uk/news/111111

How Do I Do This?

Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:

http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111

http://www.website.co.uk/news/city/issue/the-title/111111

How can I make it so that the future changes will work too? So far I have:

RewriteEngine On
RewriteRule   ^news/(.+)$   news.php?id=$1   [L]

This still displays the full url and typing in news/111111 redirects to an error page. Please help!

user3177012
  • 587
  • 2
  • 7
  • 17
  • 1
    possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – deceze Mar 30 '14 at 09:06
  • @deceze can you answer [This](http://webmasters.stackexchange.com/questions/59543/apache-url-rewriting-not-working) question ? – CS GO Mar 30 '14 at 09:19
  • @deceze can you explain this for MY example? I've tried several tutorials/explained blogs but none seem to work – user3177012 Mar 30 '14 at 09:34

1 Answers1

0

Adding the following to your htaccess should work:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]

RewriteRule ^news/(.*) news.php?id=$1 [QSA]

The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111

and below will change

RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]

RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]

http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111

The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.

In city=London, London will be contained in %1 etc

The second RewriteRule will allow you to find the id used.

Howli
  • 11,217
  • 17
  • 44
  • 70
  • Thnak you! This seems to work to some extent however I'm getting redirected to an error page once inside of news - I think due to the fact that I have an $id = $_GET["id"]; statement. How can I sort that out? – user3177012 Mar 30 '14 at 09:41
  • This works great for the redirection however it's redirecting to an error page because I have 'if(isset($_GET["id"])){ $id = $_GET["id"]; } else{ header("Location: redirect.php?type=error");' - How can I ensure that it picks up the ID? – user3177012 Mar 30 '14 at 09:50