doujia1871 2017-07-25 14:35
浏览 80
已采纳

.htaccess从路径重定向到文件

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

  • 写回答

1条回答 默认 最新

  • dongmou3615 2017-07-25 14:49
    关注
    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分