I want to create a bunch of files without an extension showing at the end. The easiest way to do that was to do this:
/usa/index.php
/usa/alaska/index.php
/usa/alabama/index.php
/usa/california/index.php
What I want to do is this
/usa/alaska.php
/usa/alabama.php
/usa/california.php
and have it show up as:
/usa/alaska
/usa/alabama
/usa/california
However, I have one more level I want to add to this, the cities
/usa/alaska/adak.php
/usa/alaska/anchorage.php
/usa/california/los-angles.php
I don't want the ".php" showing up, but then each state exists as both a file and a directory. What I want is an htaccess rule that serves up the file version of the file, not the directory which is the default. I also want to strip the .php off of the end of the files so the final result looks like
/usa
/usa/alaska (alaska.php)
/usa/alaska/adak (adak.php)
I know I can get close to this by creating all the directories and using index.php for each directory, but then I will have thousands of directories each with one file in it and updating is a pain in the butt. I would much rather have one directory with 1000 files in it, than 1000 directories with 1 file in it.
Please, can someone point me in the right direction and know that I am doing this for all 50 states.
Jim
what about creating just one single file:
With
you can read the current URI. Well, now if a user enters "http://domain.foo/usa/alaska" for example, he will get an 404 error of course. But to call your
index.php
instead, you could write this line to the.htaccess
:Now the
index.php
receives everything what is written to the URI and you can match the result and include files or handle errors.But maybe there is a better solution with .htaccess only, don't know. :)
I would also suggest using a single php (e.g.
index.php
) file and redirecting all urls starting withusa
to it, instead of separating them in different directories and files. The you'd need a couple of rewrite rules like the followingSo then in your
index.php
you'd only need to check the$_GET
parameters.Update:
If you don't feel comfortable enough to use a database and pull the needed data from there you could always use the parameters to dynamically include/require the needed files. Something like this
But to answer your original question nevertheless you could use the following
.htaccess