htaccess - creating directories and files of the s

2019-08-17 04:13发布

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

2条回答
Emotional °昔
2楼-- · 2019-08-17 04:45

what about creating just one single file:

/usa/index.php

With

$_SERVER["REQUEST_URI"]

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:

ErrorDocument 404 /usa/index.php

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. :)

查看更多
ら.Afraid
3楼-- · 2019-08-17 05:05

I would also suggest using a single php (e.g. index.php) file and redirecting all urls starting with usa to it, instead of separating them in different directories and files. The you'd need a couple of rewrite rules like the following

RewriteEngine On
RewriteRule ^usa/([^/.]+)$ index.php?state=$1 [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ index.php?state=$1&city=$2 [L]

So 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

<?php
    $source = ''; //or the 'ROOT' directory
    if(isset($_GET['state'])) $source .= $_GET['state'].'/';
    if(isset($_GET['city'])) $source .= $_GET['city'].'.php';
    include($source);  // here $source would be something like 'alaska/adak.php'
                       // and is assumed that the dir 'alaska' is on the same 
                       // level as 'index.php'
?>

But to answer your original question nevertheless you could use the following .htaccess

RewriteEngine On
RewriteRule ^usa/([^/.]+)$ usa/$1.php [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ usa/$1/$2.php [L]
查看更多
登录 后发表回答