【问题标题】:.htaccess URL redirect but not displaying index page.htaccess URL 重定向但不显示索引页面
【发布时间】:2013-10-20 00:24:05
【问题描述】:

在为我的问题寻找答案后,我仍然对此感到困惑。我正在开发一个允许用户创建个人资料的小型社交脚本,但我希望他们的个人资料 URL 简短。我编辑了 .htaccess 文件,它运行良好!

例如 www.site.com/username 重定向到 www.site.com/profile.php?user=username

但是,当您只是转到索引页面时。它显示 profile.php 但带有一个空的 GET 变量...

这是我的 .htaccess 代码

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

提前致谢!

【问题讨论】:

  • 这个.htaccess 的位置是什么?profile.php and index.php 文件在哪里?
  • 主目录(/public_html)。他们需要在不同的目录中吗?真的希望使它成为 www.site.com/username 而不是 www.site.com/profile/username
  • Are profile.php and index.php also in the main directory(/public_html).?
  • 是的,所有文件都在同一个目录下。
  • 我会说:改用^(.+)$。这要求至少存在 1 个字符。假设服务器已正确配置为在您请求目录时显示index.php 等,它现在应该显示http://site.url/ 的索引页面。

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

我认为,您在访问地址时遇到了问题:http://www.site.com/(不是 www.site.com/index.php)。并且 .htaccess 看到文件“”不存在并重定向到 www.site.com/profile.php?user= (使用空参数用户)。尝试修改 .htaccess (使用^(.+)$ 而不是^(.*)$):

RewriteEngine One
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ profile.php?name=$1 [QSA,L]

【讨论】:

    【解决方案2】:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)/?$ profile.php?name=$1 [QSA,L]
    

    拥有/? 还可以防止任何尾随斜杠(如果存在)。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多