dtnpf35197 2016-03-19 14:56
浏览 37
已采纳

漂亮的URL通过htaccess使用任意数量的参数

Actually i have this URL:

http://www.example.com/index.php?site=contact&param1=value1&param2=value2&param3=value3

But i want to have this URL format:

http://www.example.com/contact/param1:value1/param2:value2/param3:value3

So the "contact" goes to variable $_GET["site"] and rest of parameters should be able to access via $_GET["param1"], $_GET["param2"] etc. The problem is, it has to work with any number of parameters (there could be param4 or even param50 or any other name of parameter). Is it possible via htaccess to cover all these cases?

  • 写回答

5条回答 默认 最新

  • douyuan3842 2016-04-02 00:05
    关注

    This is entirely doable using some creative htaccess and PHP. Effectively what you are doing here is telling Apache to direct all page requests to index.php if they are not for a real file or directory on the server...

    ## No directory listings
    IndexIgnore *
    
    ## Can be commented out if causes errors, see notes above.
    Options +FollowSymlinks
    Options -Indexes
    
    ## Mod_rewrite in use.
    
    RewriteEngine On
    
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    RewriteCond %{REQUEST_URI} !^/index\.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php [L]
    

    After this all you need to do is go into PHP and access the full user requested URL structure using the $_SERVER['REQUEST_URI'] superglobal and then break it down into an array using explode("/", $_SERVER['REQUEST_URI']).

    I currently use this on a number of my sites with all of the sites being served by index.php but with url structures such as...

    http://www.domain.com/forums/11824-some-topic-name/reply

    which is then processed by the explode command to appear in an array as... 0=>"forums", 1=>"11824-some-topic-name",2=>"reply"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了