drhzn3911 2012-09-20 19:23
浏览 5
已采纳

无法让mod_rewrite工作

I am trying to get url from:

192.168.0.1/movie-page.php?id=123

to:

192.168.0.1/movie/movie-name

or even (for now):

192.168.0.1/movie/123

I've simplified it by using this url (to get something working):

192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/

my .htaccess file:

RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]

What am I doing wrong? I've tried many combinations but no luck and my patience is running out...

I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.

  • 写回答

2条回答 默认 最新

  • doutongxuan1614 2012-09-20 19:51
    关注

    If you want to rewrite: 192.168.0.1/movie-page.php?id=123 too

    192.168.0.1/movie/movie-name or 192.168.0.1/movie/123

    Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
    

    So a better method is to route everything through the rewrite:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
    

    Then handle the route by splitting the $_GET['route'] with explode()

    <?php 
    //192.168.0.1/movie/movie-name
    $route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
    
    if(!empty($route)){
        //example
        $route[0]; //movie
        $route[1]; //movie-name
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器