happypete Posted September 8, 2013 Share Posted September 8, 2013 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 etcI'm thinking to use a single page (item.php) and populate it based on the $_GET['id'] ie: item.php?id=1Is 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 butdisplay 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...? Quote Link to comment https://forums.phpfreaks.com/topic/281987-clean-urls-for-multiple-pages/ Share on other sites More sharing options...
cataiin Posted September 8, 2013 Share Posted September 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281987-clean-urls-for-multiple-pages/#findComment-1448738 Share on other sites More sharing options...
happypete Posted September 8, 2013 Author Share Posted September 8, 2013 (edited) 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 September 8, 2013 by happypete Quote Link to comment https://forums.phpfreaks.com/topic/281987-clean-urls-for-multiple-pages/#findComment-1448741 Share on other sites More sharing options...
Solution cataiin Posted September 8, 2013 Solution Share Posted September 8, 2013 (edited) 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. Edited September 8, 2013 by cataiin Quote Link to comment https://forums.phpfreaks.com/topic/281987-clean-urls-for-multiple-pages/#findComment-1448748 Share on other sites More sharing options...
happypete Posted September 8, 2013 Author Share Posted September 8, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/281987-clean-urls-for-multiple-pages/#findComment-1448752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.