【问题标题】:Redirect variable in url to a specific page in apache将url中的变量重定向到apache中的特定页面
【发布时间】:2015-11-07 11:12:12
【问题描述】:

我正在尝试将用户重定向到个人资料页面 (profile.php)

这是链接

https://dev-website.com/name_of_user

文件位于

~/profile.php

如果我运行此链接,它可以工作

https://dev-website.com/profile.php?username=name_of_user

这是我的脚本

Options +FollowSymlinks
RewriteEngine On

Options -Indexes

RewriteCond %{REQUEST_URI} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !index.html$
RewriteRule ^(.+[^/])(.*)$ https://dev-website.com/profile.php?username=$1 [L]

我收到此错误

找不到

在此服务器上找不到请求的 URL /name_of_user

Apache/2.4.7 (Ubuntu) 服务器位于 dev-website.com 端口 443

请帮忙

【问题讨论】:

  • ^(.+[^/])(.*)$我看不懂...应该是什么意思?
  • 我建议从^(.*)$开始调试。
  • 还有很多其他的重定向inbeteen。这只是 htaccess 的一部分。无论如何我已经找到了解决方案看看我的答案

标签: php regex apache


【解决方案1】:

更改您的 .htaccess :

Options +FollowSymlinks
RewriteEngine On

Options -Indexes

RewriteCond %{REQUEST_URI} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !index.html$
RewriteRule ^(.+[^/])(.*)$ https://dev-website.com/profile.php [L] # <- change

在您的 profile.php 中,获取 username,然后执行用户特定任务,例如:

<?php 

  if( isset($_GET['username'] ) {
     $username = $_GET['username'];
     // do whatever you want as user specific staff here
  }

所以,链接:https://dev-website.com/profile.php?username=name_of_user 工作正常。

【讨论】:

    【解决方案2】:
    Options -Indexes -MultiViews +FollowSymlinks
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !-d 
    RewriteCond %{SCRIPT_FILENAME} !-f 
    RewriteCond %{SCRIPT_FILENAME} !index.html$
    
    RewriteRule ^(.+[^/])(.*)$ profile.php?username=$1 [L,NC,QSA]
    

    【讨论】:

      【解决方案3】:

      如果您想在身份验证后执行此操作,您应该执行以下操作:

      if(isValidateUser($username, $pass)){
         header('HTTP/1.0 301');
         header('Location: https://dev-website.com/profile.php?username='.$username));
         die();
      }
      

      但如果您坚持使用 .htaccess,请在适当的条件下尝试使用此规则:

      RewriteRule ^(.+[^/])(.*)$ /profile.php?username=$1 [R=301,L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 2018-03-04
        相关资源
        最近更新 更多