简体   繁体   English

我如何获得我的htaccess以使我的网址看起来像foo.bar/user的此代码段

[英]how can i get my htaccess to make my url look like foo.bar/user with this snippet i have

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. 我将其添加到我的.htaccess但是现在我对如何访问这些URL感到困惑。

in my index.php when a user logs in i have tried to redirect the user using 在我的index.php当用户登录时,我尝试使用

userpage.php?user=s2xi

but the url parses as www.foo.bar/userpage.php?user=s2xi and not www.foo.bar/s2xi 但网址解析为www.foo.bar/userpage.php?user=s2xi而不是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 我在网站上使用Smarty模板引擎,并且在属于成员文件的目录中有“主题”

www.foo.bar/users/s2xi/themes 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. 但我希望www.foo.bar/s2xi指向其他任何人都可以查看的人员个人资料页面,而不是他们的帐户页面。

You're missing the .php in your RewriteRule , if that's verbatim - eg, userpage? 你错过了.php在你RewriteRule ,如果这是逐字-例如, userpage? => userpage.php? => 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; 如果您切换到使用单独的URI格式的用户页面(如/user/foo ),你不会有冲突; 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. 但按照目前的情况,使用.htaccess重写该格式的URL可能会导致应用程序其他许多部分出现问题。

To do it with a separate URI format, change your last .htaccess line (the RewriteRule ) to: 要使用一个单独的URI格式,这样做,改变你的最后的.htaccess行( RewriteRule )来:

RewriteRule ^user/(.+)/?$ userpage.php?user=$1 [NC,L]

may want to consider adding QSA as well. 可能还需要考虑添加QSA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM