简体   繁体   中英

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. For example, I have hierarchy like :- science->biology->zoology->dermatology . and domain is www.mydomain.com . So

If I click on science , Url should be www.mydomain.com/science .

Under science If I click on biology , Url should be www.mydomain.com/science/biology .

Under biology If I click on zoology , Url should be 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

 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.

There is a little disorientation in your thoughts.

Any kind of url you are generating in the PHP without any restrictions.

And .htaccess (and mod_rewrite in particular) used only for redirection of request (transparent for users) to the particular php-file.

So, yes, you can do url like this: http://www.example.com/science/biology/zoology and your .htacces should be like this:

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.

RewriteCond %{REQUEST_FILENAME} !-f is needed to test that requested uri didn't point to real file.


UPDATE: If you want to use $_GET array in the branch.php you can do the following:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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