[英]Vanity URLs using PHP and htaccess
I am developing an app using PHP and I would like my users to be able to use their own vanity URLs. 我正在使用PHP开发一个应用程序,我希望我的用户能够使用他们自己的虚荣URL。
In this format: 采用以下格式:
http://example.com/username
For example: 例如:
http://example.com/myCoolUsername
If I use this URL now the server thinks it is a folder but I know I should use a .htaccess file in a way but I do not know what content I should use in that file. 如果我现在使用此URL,服务器认为它是一个文件夹,但我知道我应该以某种方式使用.htaccess文件,但我不知道我应该在该文件中使用哪些内容。
This rule will rewrite http://domain.com/username
to user.php
with username
as GET parameter ( name
). 此规则将
http://domain.com/username
重写为user.php
, username
名为GET参数( name
)。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /user.php?name=$1
To test, create a file named user.php
in your directory root ant put that in your file: 要测试,请在目录root ant中创建一个名为
user.php
的文件,并将其放入您的文件中:
Access http://domain.com/blablaName123
and if everything works fine you should see string(13) "blablaName123"
访问
http://domain.com/blablaName123
,如果一切正常,你应该看到string(13) "blablaName123"
This assumes your index.php
receives a $_GET['username']
to map to their vanity URL: 这假设您的
index.php
收到$_GET['username']
以映射到他们的虚荣URL:
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the first part after the `/` into a `username` parameter
RewriteRule ^(.+)$ /index.php?username=$1 [L,QSA]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.