Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Site Structure for clean URLS

I'm currently learning PHP and trying to convert a regular HTML site. Should I be creating a separate subdirectory for each page to keep the urls clean? For example my-domain.com/workshops/index.php and my-domain.com/workshops/workshop2/index.php my-domain.com/workshops/workshop3/index.php

or should I be using a rewrite to get this a different way?

Thanks for your help

11 Answers

Kevin Korte
Kevin Korte
28,149 Points

You could do that, if that's how you wanted your URL structure to work.

If you wanted to have workshop1, workshop2, and workshop3 all in the workshop subfolder, you could do that, and then use rules in a .htaccess file placed inside the workshop folder to display clean URLs.

That might look like my-domain.com/workshops/workshop1 ....etc.

Okay I understand. But lets say off of workshops I want pages like

my-domain.com/workshops/workshopNY/ my-domain.com/workshops/workshopLA/ my-domain.com/workshops/workshopFL/

is the optimal way to create sub directories for these? Or to just do it like my-domain.com/workshops/workshopNY.php my-domain.com/workshops/workshopLA.php my-domain.com/workshops/workshopFL.php

I just want to make sure I set my page up the best way possible.

Kevin Korte
Kevin Korte
28,149 Points

It would drive me nuts to have that many subfolders and index.php files, but you could do it.

I'd rather just use mod rewrite rules in a .htaccess file inside of the workshops subfolder, so than you could have workshopNY.php, workshopLA.php and workshopFL.php all in the same workshops subfolder, but use the mentioned mod rewrite rules to remove the .php file extensions, so you have clean URLS.

Follow the links posted by Elliott. They explain how to do it.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

How different are your individual workshop pages? Are they all essentially the same with just a little bit of different data in each (maybe like date, location, etc.)? Or do they each completely different HTML?

There's going to be 5 sections of the site that I can group in individual sub-directories. All the html will be pretty different. Should I just use a rewrite to remove the ".php" extension after the files?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

One more question: why are they PHP files and not static HTML files? Are they pulling content from somewhere? Are they sharing template parts?

They are sharing headers and sidebars

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Cool. You can't just remove the extension from the file name because the server then won't execute the PHP code inside of them. (You could probably configure the server to that, but it would be a hassle and isn't worth it.)

I would probably create separate subfolders with index.php files for each of them:

  • You don't want the web addresses to have .php in them; you want clean URLs.
  • The web server natively thinks of http://domain.com/subfolder/ as a subfolder, so it doesn't require any rewrite rules that could be difficult to maintain.

The drawbacks?

  • You end up with a lot of subfolders!
  • You end up with a lot of files named index.php!

I wouldn't use rewrite rules unless you were going to use a single template file for structured data. I do that with the shirts in the Shirts 4 Mike project. Most of the pages are index.php files in a subfolder, but the Shirt Detail pages all use one shirt.php template file filled with different data about each shirt.

Does that help?

Yes that helps a lot. Thank you.

I just wanted to make sure I was going to do everything correctly.

Kevin Korte
Kevin Korte
28,149 Points

Randy saves the day. He's much smarter than me! Take his advice :)