使用.htaccess在URL中使用用户名进行重定向 [英] Redirect using username in URL with .htaccess
问题描述
我想进行重定向,即用户在URL中输入用户名并重定向到其个人资料,例如-> example.com/user/joe
重定向 example.com/user/profile.php?username=joe
并明显显示其个人资料。
I want to do a redirection where a user enters their user name in the URL and redirects to their profile, like this-> example.com/user/joe
redirects example.com/user/profile.php?username=joe
and obviously shows their profile.
我一直在寻找如何执行此操作的方法,但仍无法使其正常工作。
我将如何做到这一点?我应该将.htaccess文件放在哪里?
I've looked around on how to do this but still haven't managed to get it working. How will I be able to do this? And where am I supposed to place my .htaccess file?
- root
- admin
- 包括
- 公共
- 用户
- profile.php
推荐答案
在您的目录中,看起来 public目录是您网站的文档根目录。因此,将.htaccess文件放在此处。
From your directory it looks like 'public' directory is the document root for your website. So, place the .htaccess file there.
没有一个经过测试。但这应该可以帮助您摆脱困境。我将使用mod_rewrite实现此目的。
None of this is tested. But this should get you off the ground. I would use mod_rewrite to achieve this. Something like this should go into your .htaccess file.
RewriteEngine On RewriteRule ^user/(.+)$ user/profile.php?username=$1 [R,L]
这应该可以解决您的问题。
[R]
标志将导致Apache HTTPD要求浏览器重定向并访问新URL。如果您希望重定向是内部的,而用户不会对此有所了解,则不要使用[R,L]
标志。This should solve your problem. The
[R]
flag would cause Apache HTTPD to ask the browser to redirect and visit the new URL. Don't use the[R,L]
flag if you want the redirect to be internal without the user coming to know anything about it.话虽如此,这就是我的真正想法。
Having said that, this is how I would really do it.
RewriteEngine On # Canonicalize /user/foo to /user/foo/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^profile/([^/]+)$ /profile/$1/ [R=301,L] # Map /user/foo/ to foo's profile RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^profile/([^/]+)/$ profile/index.php?p=$1 [QSA]
RewriteCond%{REQUEST_FILENAME}!-f
和RewriteCond%{REQUEST_FILENAME }!-d
将确保当用户请求服务器中存在的有效文件或文件夹资源时,不要理会所有这些重定向规则,请继续并立即提供该文件或文件夹。RewriteCond %{REQUEST_FILENAME} !-f
andRewriteCond %{REQUEST_FILENAME} !-d
would be to make sure that when a user has requested for a resource that is a valid file or folder present in the server, just don't bother about all these redirect rules, go ahead and serve that file or folder right away.第一个
RewriteRule
是确保如果用户忘记了斜杠,服务器会要求浏览器重定向到带有斜杠的URL。这是Apache HTTPD默认情况下的行为方式(也许是由于mod_dir造成的),我也会尝试在此处保留相同的行为。The first
RewriteRule
is to make sure that if the user forgets the trailing slash, the server asks the browser to redirect to the URL with a trailing slash. This is how Apache HTTPD behaves by default (perhaps because of mod_dir) and I would try to preserve the same behaviour here as well.第二个
RewriteRule
确保对http://example.com/user/foo/
的请求是有效的URL,但http://example.com/user/foo/bar/
不是。[QSA]
标志可确保保留添加到URL的所有参数。The second
RewriteRule
makes sure that a request tohttp://example.com/user/foo/
is a valid URL buthttp://example.com/user/foo/bar/
is not. The[QSA]
flag ensures that any parameters added to the URL are preserved.不包含
[QSA]
标志,http://example.com/user/foo/?ref=related+articles
会在内部重定向到http://example.com/user/profile.php?user=foo
。有了该标志,它将重定向到我更喜欢的http://example.com/user/profile.php?user=foo&ref=related+articles
。Without the
[QSA]
flag,http://example.com/user/foo/?ref=related+articles
would internally redirect tohttp://example.com/user/profile.php?user=foo
. With that flag, it would redirect tohttp://example.com/user/profile.php?user=foo&ref=related+articles
which is what I prefer.这篇关于使用.htaccess在URL中使用用户名进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 用户