2

I have my site as localhost/tutorials/rewrite/news.php on this page are titles from news articles that are in my database. Each title is a link to read the article. The link is

'<a href="read-news.php?url=' . $url . '">' . $title1. '</a>'

The read-news.php page gets the url and uses it in an sql statement to get the article from the database

$url = $_GET['url'];
$sql = "SELECT * FROM news WHERE url = '$url'";

My link on the news.php page and the url on the read-news.php looks like this

localhost/tutorials/rewrite/read-news.php?url=the-news-today

How can i get it to look like

localhost/tutorials/rewrite/read-news/the-news-today.php

I have used the following htaccess code which by looking at other examples i thought should be enough to fix it

RewriteRule ^read-news/(.*).php /read-news.php?url=$1 [PT]

Any help please

Shef
  • 44,808
  • 15
  • 79
  • 90
Pierce McGeough
  • 3,016
  • 8
  • 43
  • 65

2 Answers2

0

Do it how Wordpress does it. Rewrite everything into index.php and parse the url from script.

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • how is that done? are there any tutorials on how to do it easily? – Pierce McGeough Mar 21 '12 at 23:30
  • I'm not sure on tutorials but it surely is easy. `RewriteRule . /index.php [L]` this rule sends all traffic to **index.php**. In index.php you can have `$path = explode('/',$_SERVER['REQUEST_URI']);` and for `http://localhost/tutorials/rewrite/read-news` $path[0] would be 'tutorials', $path[1] would be 'rewrite', etc. Does this make sense? – Silviu-Marian Mar 22 '12 at 07:47
0

edit: your RewriteRule needs some work too. try this:

RewriteRule ^read-news/(.*)\.php$ /read-news.php?url=$1 [PT]

(you need to use a "$" to signal the end of your rewrite and escape the . before "php" with a backslash)

Also, you should link to your pages how you want them displayed:

href="localhost/tutorials/rewrite/read-news/the-news-today.php"

The .htaccess won't magically change the urls for you.

Also if you want to be able to access your query string variables, i would add QSA to your flags: [PT,QSA]

that way you can still access your url variable with $_GET['url'].

n1ch0la5
  • 89
  • 1
  • 1
  • 5