嗨,大家好继续现场 - 我正在构建一个基本上为所有用户创建帐户的Web应用程序。目前其设置如下文件结构:
root/index.php
root/someotherfile.php
root/images/image-sub-folder/image.jpg
root/js/somejs.js
当用户创建帐户时,他们会选择固定的组名,然后用户就可以加入该组。最初我想在登录屏幕上有一个额外的文本框来输入用户所属的登录组。 BUt我希望在这种情况下有类似虚拟文件夹的东西:
root/group-name/index.php
我听说可以用apache mod重写来完成,但我不知道怎么做 - 这里有任何帮助吗?
基本上没有像& group-name = yourGroupName这样的东西附加到每个页面上,我只想要上面的一些东西。
答案 0 :(得分:2)
您可以在.htaccess文件中配置Mod Rewrite,例如
根/ htaccess的:
RewriteEngine On
#make sure that urls which already represent files or directories are not rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#update this if the files are not actually in /root
RewriteBase /root
RewriteRule ^([^/]+)/(.*\.php)$ $2?group-name=$1 [L,QSA]
因此,任何匹配/[groupname]/[php file]
的请求都应重新写入/[php file]?group-name=[groupname]
。
L
表示这是最后一条规则,因此如果匹配则不继续运行其他规则QSA
表示附加原始查询字符串(因此其他GET参数仍会传递给PHP脚本)您可以将匹配组名([^/]+)
的模式替换为更具限制性的模式,例如: ([a-zA-Z0-9_-]+)
。
您还可以将配置放在一个apache配置文件中,这比在每个请求中解析.htaccess文件要快。