URL目录作为带有htaccess的变量 [英] URL directory as variable with htaccess

查看:64
本文介绍了URL目录作为带有htaccess的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际的.htaccess是

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([_0-9a-zA-Z-]+)? index.php?cl=$1 [L]

,当我键入sub.domain.com/client1之类的东西时,它会很好地工作(然后我在PHP代码中正确获得了$ cl变量).问题是,当我添加最后一个结尾斜杠(例如sub.domain.com/client1/)时,我在我的var中得到了"client1/"!

我该如何解决?我知道htaccess中的第二个条件是查找现有目录并跳过重写规则,但是服务器上没有"client1"目录!

解决方案

问题是,当我添加最终的斜杠(例如sub.domain.com/client1/)时,我在var中得到了"client1/"!"

这是预期的行为.之后,您需要通过PHP去除斜杠:

$cl = trim( $_REQUEST['cl'], '/' );

或者您将值分解为所有文件夹(如果在斜杠后有另一个名称)

$cl = explode( '/', $_REQUEST['cl'] );

my actual .htaccess is

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([_0-9a-zA-Z-]+)? index.php?cl=$1 [L]

and it works good when i type something like sub.domain.com/client1 (then i get the $cl variable in my php code properly). Problem is when i add a final trailing slash like sub.domain.com/client1/ then i get "client1/" in my var!

How can i fix it? I know that the second condition in my htaccess looks for existing directories and skips the rewrite rule but there is no "client1" dir on my server!

解决方案

" Problem is when i add a final trailing slash like sub.domain.com/client1/ then i get "client1/" in my var!"

That is the expected behavior. You need to strip the slash via PHP afterwards:

$cl = trim( $_REQUEST['cl'], '/' );

or you explode the value to get all folders (if there is another name following after the slash)

$cl = explode( '/', $_REQUEST['cl'] );

这篇关于URL目录作为带有htaccess的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆