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

时间:2017-01-20 10:00:51

标签: php .htaccess

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

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

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

最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

首先,您必须重写对index.php的调用。这是一个经典的,可以在任何地方找到

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

下一步是访问URI本身,您已使用$_SERVER['REQUEST_URI']找到了自己。

答案 1 :(得分:0)

您可以将文件apartment-mike.php添加到index.php文件夹的accomodation,然后将以下行添加到.htaccess文件

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

删除扩展名出现在uri中 别忘了添加

header("location:index.php");
如果您希望将用户传递给索引

,请

apatement-mike.php

相关问题