简体   繁体   中英

.htaccess rewrite to access php include in subfolder

I have this URL that give me two main parameters, folder and page, and som other tahat can variable to be present or not:

http://www.domain.com/adm/master.php?f=folder&p=page&otherparam1=somthing&otherparm2=somthing&otherparm3=somthing[...]

in my master.php-file I include a php-file based on these parameters:

$folder = $_GET['f'];
$page = $_GET['p'];
if(empty($page)) {
    $page = 'dashboard';
}
$path = $folder.'/'.$page;
include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';    

The otherparam is to be multiple parameters given i the URL.

I want to rewrtite the URL to show somthing like this:

www.domain.com/adm/folder/page/somthing/somthing/somthing[...]

I have searched the web, but not been able to find a solution.

If you want to do it with simple .htacces then you can use following .htacces code:

RewriteEngine on
RewriteCond $1 !^(index\.php|css|favicon.ico|fonts|images|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Basically what this code do is overwrite your request to your domain (ex: localhost/mysite), instead accessing folder or file it will access your index.php . Except for index.php , favoicon.ico, robots.txt files and css,fonts,image and js folder or what you define in second line on my code above.

Try this in your Root/.htaccess

RewriteEngine On

RewriteRule ^adm/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /adm/master.php?f=$1&p=$2&otherparam1=$3&otherparm2=$4&otherparm3=$5 [NC,L]

$1 ,$2,$3... contains whateve value was matched in regex capture groups "([^/]+)".

This will rewrite

example.com/adm/folder/page/foo/bar/foobar

to

 example.com/adm/master.php?f=folder&p=page&otherparam1=foo&otherparm2=bar&otherparm3=foobar

without changing the url in browser address bar.

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