我怎样才能让我的 htaccess 使我的 url 看起来像 foo.bar/user 以及我有的这个片段
我一直在努力让我的网址更加用户友好,我想出了这个设置
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage?user=$1 [NC,L]
,我将其添加到我的 .htaccess
中,但我现在对如何访问感到困惑这些网址。
在我的 index.php
中,当用户登录时,我尝试使用重定向用户,
userpage.php?user=s2xi
但 url 解析为 www.foo.bar/userpage.php?user=s2xi
而不是 www.foo.bar/s2xi
并且还尝试将此作为检查用户是否存在(有更好的方法吗?)
if($_GET['user'] != $_SESSION['username']){
header("Location: no_user.php");
}else{
//load page
}
我在我的网站上使用 Smarty 模板引擎,并且我有属于成员文件
www.foo.bar/users/s2xi/themes
的目录中的“主题” ,但我希望 www.foo.bar/s2xi 指向其他人都可以查看的个人资料页面,而不是他们的帐户页面。
I have been trying to get my urls to be more user friendly and I have come up with this set up
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage?user=$1 [NC,L]
I added this to my .htaccess
but I'm now i'm confused as to how to access these urls.
in my index.php
when a user logs in i have tried to redirect the user using
userpage.php?user=s2xi
but the url parses as www.foo.bar/userpage.php?user=s2xi
and not www.foo.bar/s2xi
and also tried this as a check to see if user exists (is there a better way?)
if($_GET['user'] != $_SESSION['username']){
header("Location: no_user.php");
}else{
//load page
}
I am using the Smarty template engine on my site and I have my 'themes' in directories that belong to members file
www.foo.bar/users/s2xi/themes
but i want www.foo.bar/s2xi to point to the persons profile page that is viewable by everyone else and not their accounts page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是逐字记录的话,您在
RewriteRule
中缺少.php
- 例如userpage?
=>;userpage.php?
。但是,除非您使用框架来帮助您区分路由,否则您将遇到一些问题。如果您切换到对用户页面使用单独的 URI 格式(例如
/user/foo
),则不会出现冲突;但就目前情况而言,使用 .htaccess 以该格式重写 URL 可能会导致应用程序的许多其他部分出现问题。要使用单独的 URI 格式执行此操作,请将最后一个 .htaccess 行(
RewriteRule
)更改为:可能还需要考虑添加
QSA
。You're missing the
.php
in yourRewriteRule
, if that's verbatim - eg,userpage?
=>userpage.php?
.However, you're going to run into some problems with this unless you're using a framework to help you distinguish between routes. If you switched to using a separate URI format for user pages (eg
/user/foo
) you wouldn't have conflicts; but as it stands currently, using .htaccess to rewrite your URLs in that format could potentially cause problems with many other parts of your app.To do it with a separate URI format, change your last .htaccess line (the
RewriteRule
) to:may want to consider adding
QSA
as well.