简体   繁体   中英

.htaccess redirect from path to file

I want to set up a custom template system for a website that I'm working on, so I want to redirect all requests to a single file. If the user visits an URL like http://www.mywebsite.de/products/software/ I want to redirect his request to a file like index.php , so I could do something like the following:

$look_at = $_GET['first_level']; //for example: products
if($look_at == 'products') {
    $look_at = $_GET['second_level']; //here: software
    if($look_at == 'software') {
        //show software specific stuff
    } else if ($look_at == 'hardware') {
        //show hardware specicif stuff
    } else {
        //show error message
    }
} else {
    //show error message
}

Then, I could use include to, well include, other html files into index.php depending on what is being requested.

In the end, a request like http://www.mywebsite.de/products/software/ is supposed to be the same as http://www.mywebsite.de/index.php?first_level=products&second_level=software

RewriteEngine on

RedirectMatch 403 /\..*$
# If the directory or file exists, use them directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise, send a request to the index.php file
RewriteRule . index.php

Next in PHP example:

$URI = $_SERVER['REQUEST_URI'];

if (($qpos=(strpos($URI,'/?')))!==false)
  $URI=substr($URI,0,++$qpos);
elseif (($qpos=(strpos($URI,'?')))!==false)
  $URI=substr($URI,0,++$qpos);

$URI = (preg_replace('/^\/|\/$/', '',$URI));

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