.htaccess无法正常工作,更改个人资料页面?

时间:2015-04-13 07:27:42

标签: php .htaccess mod-rewrite url-rewriting

我目前在使用htaccess时遇到问题,我想使用profile.php?u = rahulkapoor作为www.domain.com/rahulkapoor,其中u代表用户名。但它运作不正常。

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9._-]+)/?$ profile.php?u=$1 [L,QSA]

这里是profile.php

<?php
if (isset($_GET['u'])) {
    $username = mysqli_real_escape_string($_GET['u']);
    if (ctype_alnum($username)) {
    //check user exists
    $check = mysqli_query($conn,"SELECT username, first_name FROM users2 WHERE username='$username'");
    if (mysqli_num_rows($check)===1) {
    $get = mysqli_fetch_assoc($check);
    $username = $get['username'];
    $firstname = $get['first_name'];    
    }
    else
    {
    echo "<meta http-equiv=\"refresh\" content=\"0; url=/index.php\">"; 
    exit();
    }
    }
}
 $get_info = mysqli_query($conn,"SELECT name,email FROM users2 WHERE username='$username'");
  $get_row = mysqli_fetch_assoc($get_info);
  $name = $get_row['name'];
  $email = $get_row['email'];
?>

当一些人输入错误的用户名时,它应该重定向到索引页面,而不是显示任何参数的页面。

1 个答案:

答案 0 :(得分:0)

试试这个

 RewriteEngine On
 RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-d 
   RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([a-zA-Z0-9._-]+)/?$ /profile.php?u=$1 [L,QSA,NC]

请务必将目标网址www.example.com更改为您的域名。