【问题标题】:How to use .htaccess to rewrite URL's with @username如何使用 .htaccess 用 @username 重写 URL
【发布时间】:2021-04-13 12:31:25
【问题描述】:

我有一个 PHP 配置文件系统。 如果你想访问他们的个人资料,你必须转到 /profile.php?user=username 我希望能够重写这个 /@username。

目前,我的 .htaccess 代码是这样的:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ - [F]
    RewriteRule ^@(.*)$ profile.php?user=$1 [L,QSA]

已修复:我必须在我的 apache 配置中将 AllowOverride 更改为 All。

<Directory /path/to/directory>
   AllowOverride All
</Directory>

并将我的 htaccess 文件更改为:(感谢回答此问题的人)

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^@(.*)/?$ profile.php?user=$1 [L]

【问题讨论】:

  • 不确定为什么您的 htaccess 文件中有 RewriteRule ^(.*)$ - [F] 规则。这意味着它将禁止任何满足您提到的两个条件的页面。恕我直言,这不应该出现在您的规则文件中(至少对于您在问题中提出的重写而言)。

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

根据您显示的示例,您能否尝试以下操作。考虑到您正在点击像 http://localhost:80/@test" 这样的 URL,您希望它由 http://localhost:80/profile.php?user=$1 提供服务

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^@(.*)/?$ profile.php?user=$1 [L]

正如 cmets 中提到的,你不想使用你的 [F] 规则显示示例 htaccess 所以我已经从这里删除了。

【讨论】:

  • @PrestonCammarata,如果您的 profile.php 与 htaccess 文件所在的级别相同,请告诉我?
  • 是的。它是。如果有帮助,我会在 digitalocean vps 上使用 apache 运行此 Web 服务器。
  • @PrestonCammarata,我觉得规则很好,请您确认一下您点击的是哪个示例 URL。
  • website.com/@user
  • 我编辑了我的 /etc/apache2/sites-available/mywebsitehere.conf 文件,并在这两个标签之间添加了 AllowOverride All 现在它工作!!
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 2020-04-09
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多