doudui1850 2017-07-27 23:01
浏览 27

如何使用htaccess更改php中url目录的名称?

How can I do to change the name of the url of my site which is: www.example.com/user/panel.php to www.example.com/username/panel.php where the "username" is unique for each user , And for each login would be the name of the user from database, as it is in jsfiddle.net, could they help me?

  • 写回答

1条回答 默认 最新

  • dtcpvz8162 2017-07-27 23:21
    关注

    Personally I would not use .htaccess for this ( specifically )

    that said most the time people do it this way

    RewriteEngine On
    RewriteRule ^users/([-a-zA-Z0-9_]+)/?$ index.php?user=$1 [L]
    

    So if you had a url like

     www.yoursite.com/users/someguy
    

    Then it would pass it to apache ( and php ) as

    www.yoursite.com/index.php?user=someguy
    

    Then in PHP you could access it just using $_GET[user].

    Now ignoring security concerns I may have ( you shouldn't rely on user input to tell who they are, they can lie about it) for this I would use what I call the URI method ( not URL ) a URI is an imaginary path. This is also the method employed by many MVC systems. So for this I will start with the URI

     www.yoursite.com/index.php/users/someguy
    

    Notice where the index.php is ( in the middle ). Then you do a .htaccess like this

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f  #if not a real file
      RewriteCond %{REQUEST_FILENAME} !-d  #if not a real folder
      RewriteRule ^(.*)$ index.php/$1 [L] #hide the index.php
    

    So what this does is allow you to remove the index.php giving you a url like this

      www.yoursite.com/users/someguy
    

    Which is what we want, and looks basically the same as the first case.

    Then you cam use the $_SERVER['QUERY_STRING'] supper global which will give you everything past index.php

       /users/someguy
    

    And you can split that up, route it somewhere, do whatever you need to with it. Like this

     $uri = array_filter( explode('/', $_SERVER['QUERY_STRING'] ) );
    
     //$uri = [ 'users', 'someguy' ];
    

    Now the reason I like this more, is it's more flexible and it lets you use the query string the ?var part of the url for other stuff. ( like bookmarkable search forms ) ie. it feels less hacky because your not breaking the query parameters of a GET Request. Conversely, with the first method, if your .htaccess is sloppy you could make it were the query part of the URL is unusable on your site, and that just feels wrong to me.

    It also easier to maintain, because it requires no further setup for additional pretty urls

    For example: Say you want prettyfy your product. Using the first method you would have to go back to the .htaccess add at least 1 more rule in:

    RewriteEngine On
    RewriteRule ^users/([-a-zA-Z0-9_]+)/?$ index.php?user=$1 [L]
    RewriteRule ^products/(0-9_]+)/?$ index.php?product=$1 [L]
    

    Possibly even more complex levels if you have product categories

    RewriteRule ^produts/([-a-zA-Z0-9_]+)/(0-9_]+)/?$ index.php?category=$1&product_id=$2 [L]
    

    After a wile you would wind up with dozens of rules in there, some of which may not be immediately clear as to what they do. Then you realize you spelled products as produts and have to start renaming things. It's just a mess later on.

    Now using the second method you don't need to do any additional steps, besides routing it in your index page. You just put the url in

      www.yoursite.com/products/123
    

    And pull that stuff from the $_SERVER array with no further messing with rewrite rules.

    Here is a previous answer I did that outlines how to build a basic router.

    Oop php front controller issue

    Make sense.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程