htaccess的 - 创建同名的目录和文件 [英] htaccess - creating directories and files of the same name
问题描述
我要创建一批文件没有显示在最后一个扩展。要做到这一点,最简单的办法就是做到这一点:
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
我不希望.PHP显示出来,但每个国家的存在既是一个文件和目录。我要的是一个htaccess规则,担任了该文件,而不是这是默认的目录中的文件版本。我也想剥离的.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)
我知道我能接近这个创建的所有目录和使用的index.php为每个目录,但后来我将有成千上万的目录每一个文件中,并更新是一个痛苦的对接。我宁愿有一个目录有1000个文件在里面,比1000目录1文件在里面。
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.
请,可有人点我在正确的方向,知道我这样做是为所有50个州。
Please, can someone point me in the right direction and know that I am doing this for all 50 states.
吉姆
推荐答案
我也建议使用一个PHP(如的index.php
)的文件,重定向启动所有网址与美国
,而不是他们在不同的目录和文件分离到它。在你需要几个重写规则类似下面的
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]
于是在你的的index.php
你只需要检查 $ _ GET
参数。
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'
?>
但是,为了回答您的原题不过你可以使用下面的的.htaccess
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]
这篇关于htaccess的 - 创建同名的目录和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!