dongshen9686
2017-01-20 10:00
浏览 60
已采纳

.htaccess - 传递URI参数而不是列出PHP的目录

I need a suggestion using .htaccess (I presume) or any other solution for the following situation:

On the server, for example I have a folder www.mypage.com/accommodation/ which contains index.php script.

What I would like to do is that when the called URL is something like www.mypage.com/accommodation/apartment-mike (note no / in the end or extension), that the string "apartment-mike" ($_SERVER['request_uri']) is passed to the index.php script in the accommodation folder instead of trying to directory walk apartment-mike directory.

What would be the best way to do this?

图片转代码服务由CSDN问答提供 功能建议

我需要使用 .htaccess (我推测)或任何其他解决方案的建议 以下情况:

在服务器上,例如我有一个文件夹 www.mypage.com/accommodation / ,其中包含 index.php 脚本。

我想要做的是,当被叫URL类似于 www.mypage.com/accommodation/apartment-mike 时(注意没有/在 结束或扩展),字符串“ apartment-mike ”( $ _ SERVER ['request_uri'] )被传递给 index.php 在accommodation文件夹中的脚本,而不是尝试目录walk apartment-mike目录。

最好的方法是什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhong6675 2017-01-20 10:07
    已采纳

    First you must rewrite the call to index.php. This is a classic and can be found anywhere

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ /index.php [L]
    

    The next step is accessing the URI itself, which you already figured yourself by using $_SERVER['REQUEST_URI'].

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongwei4652 2017-01-20 10:10

    You can add the file apartment-mike.php, contains a ridirection to index.php, to the accomodation folder then add the following lines to the .htaccess file

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    to remove the extension from appearing in the uri don't forget to add

    header("location:index.php");
    

    to the apatement-mike.php if you want it to pass users to the index

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题