ypeels72
ypeels72

Reputation: 3

URL prettification with arbitrary number of "subdirectories"

I'm working on my first site which lives on top of a PHP/Apache stack.

My site has a logical menu structured like this:

+AAA Entry
  -AAB Entry
    -AABA Subentry
    -AABB Subentry
  -AAC Entry
    -AACA Subentry
      -AACAA Subentry
      -AACAB Subentry
    -AACB Subentry
  -AAD Entry

+BBB Entry
  -BBC Entry
    -BBCA Subentry
    etc.
+CCC Entry
+DDD Entry

My goal is to have valid URLs like these:

http://www.mydomain.com/aaa/aab/aaba
http://www.mydomain.com/aaa/aac/aaca/aacaa
http://www.mydomain.com/aaa/aac/aacb
http://www.mydomain.com/aaa/aad

http://www.mydomain.com/bbb
http://www.mydomain.com/bbb/bbc
http://www.mydomain.com/bbb/bbc/bbca

http://www.mydomain.com/ccc

I've read about mod_rewrite's RewriteRule and RewriteCond but I'm unsure as to which method to use in terms of maintainability. What if I decide to add another level to AACAA, for example? Will I have to mess with mod_rewrite over and over again?

Is it more appropriate to redirect everything to index.php and parse REQUEST_URI manually? How is this done by professionals?

Upvotes: 0

Views: 398

Answers (1)

user229044
user229044

Reputation: 239382

It's far easier to forward all URLs to your controller script, and do your routing in PHP:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* my_script.php [L]

The directs all requests for files which do not exist through my_script.php. From there, you can examine the request URI, explode it into segments delimited by forward slashes, and route to the correct file.

Upvotes: 1

Related Questions