dqrb4228 2014-07-15 08:22
浏览 26
已采纳

php,url隐藏着.htaccess

this is the real url:

http://web/app/index.php?id=1

here i have used current .htaccess and working fine with me.

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1

now the url is : http://web.com/app/index/i and working fine

but here is the problem the real url

http://web.com/app/index.php?id=1&name=abc

how can i set mention url with .htaccess for keep it short or any other good solution for hiding URL-. is it posible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.

regards

  • 写回答

2条回答 默认 最新

  • dsaob80228 2014-07-15 08:32
    关注

    If we say, good practice is to rewrite everything to one index page, like this:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :

    http://example.com/app -> $uri = /app
    http://example.com/app/sub-app -> $uri = /app/sub-app
    

    but also query string applies to the $uri variable:

    http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
    

    in this case, if you want to examine just uri part before ? question mark:

    $realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
    

    next you can examine and manipulate $uri;

    Example:

    $trimmedUri = trim($realRri, '/');
    
    if ($trimmedUri == 'app')
    {
        // you can show app page
    }
    elseif ($trimmedUri == 'contact') 
    {
        // show contact
    }
    else 
    {
        // show 404 page not found
    }
    
    
    // you can have some dynamic also also
    
    if (is_file($filePath = 'custom-page/'.$trimmedUri))
    {
        include $filePath;
    }
    else 
    {
        // 404 page
    }
    

    Final Code (regarding your current script)

    Options +FollowSymlinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    
    #rewrite php file
    RewriteRule ^([a-zA-Z]+)$ $1.php 
    #rwwrite with id
    RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2 
    #rewrite with id and name
    RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效