.Htaccess 重写用户名

问题描述 投票:0回答:3

我是 php 的初学者,主要是 htaccess 部分。这真是令人困惑。

所以我有这个文件:-

www.domain.com/profile.php?username=my_username

如何仅使用 :-

即可访问上述内容
wwww.domain.com/my_username

任何类型的指示都会有帮助

我尝试过的东西:-

RewriteEngine on
RewriteRule ^/([0-9]+)$ /profile.php?username=$1
.htaccess mod-rewrite
3个回答
2
投票

您可以在站点根目录 .htaccess 中使用这些规则集:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /foldername/

RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]

0
投票

你可以通过.htaccess来实现这一点

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/(\d+)*$ ./profile.php?username=$1

0
投票

.htaccess 文件必须获得 apache 的权限才能修改/覆盖设置,具体取决于您的主机,默认情况下可能不会启用此功能。

正如其他人提到的,.htaccess 不是 PHP 的一部分,它是一个文件,告诉 apache 如何处理某些请求以及允许访问服务器的哪些部分以及其他功能。

您可以使用 rewritecond 达到您想要的结果,只需确保重写引擎已启用。

以下是有关 .htaccess 的更多信息 https://code.tutsplus.com/tutorials/the-ultimate-guide-to-htaccess-files--net-4757

© www.soinside.com 2019 - 2024. All rights reserved.