Jump to content


Photo

Clean URL's for multiple pages

clean urls .htaccess

Best Answer cataiin, 08 September 2013 - 03:05 PM

index.php

<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<ul>
<li><a href="/1">Item 1</a></li>
<li><a href="/2">Item 2</a></li>
<li><a href="/3">Item 3</a></li>
<li>etc</li>
</ul>
</body>
</html>

item.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
$id = $_GET['id'];
$connection = mysqli_connect("localhost", "user", "pass", "database");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " .mysqli_connect_error();
}
$result = mysqli_query($connection,"SELECT * FROM items WHERE id='".$id."'");
while($row = mysqli_fetch_array($result))
{
var_dump($row);
}
mysqli_close($connection);
?>
</body>
</html>
 
and .htaccess
RewriteEngine on
RewriteRule ^([0-9]+)$ /item.php?id=$1

Sorry but my english sucks. :D

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 happypete

happypete

    Advanced Member

  • Members
  • PipPipPip
  • 116 posts

Posted 08 September 2013 - 02:08 PM

I am working on a database drive website. On my index page I have a menu like this:

<ul>
    <li> <a href="item1.php">Item 1</a></li>
    <li> <a href="item2.php">Item 2</a></li>
    <li> <a href="item3.php">Item 3</a></li>
    <li> <a href="item4.php">Item 3</a></li>
</ul>

I currently have created separate pages (item1.php / item2.php / item3.php....) Each page calls data from a database based on row 'id': 'item1.php' uses: SELECT * FROM items WHERE id = 1 etc.

All 'item' pages are identical except for the fact that they call different rows from the database: item1.php calls id=1, item2.php calls id=2 etc

I'm thinking to use a single page (
item.php) and populate it based on the $_GET['id'] ie: item.php?id=1

Is there way to write clean URL's with .htaccess and have it so if clicking on the item1.php menu in the index page that it will call item.php but
display it as item1.php and fill it will details from the database based on id=1 ?

The idea is that I will have hundreds of pages item1.php to item999.php but don't want to have to create a new page for each database row when all pages will be identical except for the fact that they import a different row from the databse. At the same time I want pages item1.php to item999.php to be index by the search engines...

I hope I have explained this properly...?



#2 cataiin

cataiin

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts
  • Locationlocalhost
  • Age:19

Posted 08 September 2013 - 02:20 PM

You can use $_GET['id]; in item.php page to get item.php?id=1 (content from current item1.php) and then with RewriteRule:

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

site.com/1 = site.com/item1.php

site.com/n = site.com/itemn.php

Ask if you have more questions.


#3 happypete

happypete

    Advanced Member

  • Members
  • PipPipPip
  • 116 posts

Posted 08 September 2013 - 02:39 PM

 

Ask if you have more questions.

 

 

Yes, sorry I'm still confused, I just can't get my head round it...

 

index.php

 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
</head>
<body>
<ul>
    <li> <a href="item1.php">Item 1</a></li>
    <li> <a href="item2.php">Item 2</a></li>
    <li> <a href="item3.php">Item 3</a></li>
    <li> <a href="item4.php">Item 3</a></li>
</ul>
</body>
</html>

item.php

<?php $item = $_GET['id']; ?>
 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
</head>
<body>
<?php echo $item; ?>
</body>
</html>

.htaccess

RewriteEngine on
RewriteRule ^([0-9]+)$ /item.php?id=$1

So if I click item1.php in the index.php page I want to be directed to item1.php  and $item should be '1'

 

item1.php doesn't exist.....


Edited by happypete, 08 September 2013 - 02:39 PM.


#4 cataiin

cataiin

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts
  • Locationlocalhost
  • Age:19

Posted 08 September 2013 - 03:05 PM   Best Answer

index.php

<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<ul>
<li><a href="/1">Item 1</a></li>
<li><a href="/2">Item 2</a></li>
<li><a href="/3">Item 3</a></li>
<li>etc</li>
</ul>
</body>
</html>

item.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
$id = $_GET['id'];
$connection = mysqli_connect("localhost", "user", "pass", "database");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " .mysqli_connect_error();
}
$result = mysqli_query($connection,"SELECT * FROM items WHERE id='".$id."'");
while($row = mysqli_fetch_array($result))
{
var_dump($row);
}
mysqli_close($connection);
?>
</body>
</html>
 
and .htaccess
RewriteEngine on
RewriteRule ^([0-9]+)$ /item.php?id=$1

Sorry but my english sucks. :D


Edited by cataiin, 08 September 2013 - 03:17 PM.


#5 happypete

happypete

    Advanced Member

  • Members
  • PipPipPip
  • 116 posts

Posted 08 September 2013 - 03:38 PM

awesome, thanks very much, it works :)

 

I added this: [a-z]+ to the .htaccess file:

RewriteEngine on
RewriteRule ^([a-z]+[0-9]+)$ /item.php?id=$1

then changed the links to this, so it would show link1 as opposed to just 1

<ul>
    <li> <a href="/link1">Item 1</a></li>
    <li> <a href="/link2">Item 2</a></li>
    <li> <a href="/link3">Item 3</a></li>
    <li> <a href="/link4">Item 3</a></li>
</ul>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com