简体   繁体   English

使用.htaccess在php中创建树结构url

[英]Create tree stucture url in php using .htaccess

I started learning basics of .htaccess file and here i wanted to know how can i create tree stucture url in php using .htaccess file. 我开始学习.htaccess文件的基础知识,在这里我想知道如何使用.htaccess文件在php中创建树结构url。 For example, I have hierarchy like :- science->biology->zoology->dermatology . 例如,我具有层次结构,例如:- 科学 - >生物学->动物学->皮肤病学 and domain is www.mydomain.com . 域是www.mydomain.com So 所以

If I click on science , Url should be www.mydomain.com/science . 如果单击“ 科学” ,则“网址”应为www.mydomain.com/science

Under science If I click on biology , Url should be www.mydomain.com/science/biology . 在“科学”下如果单击“ 生物学” ,“网址”应为www.mydomain.com/science/biology

Under biology If I click on zoology , Url should be www.mydomain.com/science/biology/zoology . 在“生物学”下如果单击“ 动物学” ,则“网址”应为www.mydomain.com/science/biology/zoology and so on. 等等。

Here is what I tried so far, created a page called as branch.php , If i click on science i am writing code in branch.php file by using url www.mydomain.com/branch/science and htacces file 这是到目前为止我尝试过的操作,创建了一个名为branch.php的页面,如果单击“科学”,我将使用URL www.mydomain.com/branch/science和htacces文件在branch.php文件中编写代码

 Options -Multiviews Options +FollowSymLinks RewriteEngine on RewriteRule ^branch/(.*) branch.php?branch_name=$1 [L] 

So I can i create url like www.mydomain.com/science/biology/zoology without creating page(branch.php) using .htacces file. 因此,我可以使用.htacces文件创建类似www.mydomain.com/science/biology/zoology之类的网址,而无需创建page(branch.php)。

There is a little disorientation in your thoughts. 你的想法有点迷失方向。

Any kind of url you are generating in the PHP without any restrictions. 您在PHP中生成的任何类型的URL没有任何限制。

And .htaccess (and mod_rewrite in particular) used only for redirection of request (transparent for users) to the particular php-file. .htaccess (尤其是mod_rewrite )仅用于将请求重定向(对用户透明)到特定的php文件。

So, yes, you can do url like this: http://www.example.com/science/biology/zoology and your .htacces should be like this: 因此,是的,您可以使用以下网址: http://www.example.com/science/biology/zoologyhttp://www.example.com/science/biology/zoology而您的.htacces应该是这样的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* branch.php [L]

Then, in the branch.php you should examine $_SERVER['REQUEST_URI'] (it will look like this /science/biology/zoology ) and make a decision what to show to the user. 然后,在branch.php您应该检查$_SERVER['REQUEST_URI'] (它看起来像是/science/biology/zoology ),然后决定向用户显示什么。

RewriteCond %{REQUEST_FILENAME} !-f is needed to test that requested uri didn't point to real file. RewriteCond %{REQUEST_FILENAME} !-f来测试请求的uri是否指向实际文件。


UPDATE: If you want to use $_GET array in the branch.php you can do the following: 更新:如果要在branch.php使用$_GET数组,可以执行以下操作:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ branch.php?main_branch=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2&sub2_branch=$3 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM