2

我正在尝试从 htaccess 创建美女 url 这是我的代码

AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /


RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule (.*)/index$ $1/ [R=301]
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /user.php?username=$1

我想显示这样的用户数据,但问题是当我运行任何其他文件(如 domain.com/contactus)时,它会重定向到 user.php?username=contactus 在这个表达式上[a-zA-Z0-9_]我也想放 (.) Dot,因为有像 john 这样的用户名.dyer to 这个表达是否正确 -[a-zA-Z0-9_.]

这是我的 user.php

<?php
$username = $_GET["username"];
$data = mysql_query("SELECT * FROM  `users` WHERE  `username` =  '$username'");
$r = mysql_fetch_array($data); 
$query_username                      = $r["username"];

if($query_username == $username)
{
include $_SERVER['DOCUMENT_ROOT'] . '/welcome/index.php';
}
?>

抱歉,我对此很陌生..我正在学习这些东西,所以请告诉我是否有更好的方法来做到这一点。谢谢

4

1 回答 1

1

你有很多问题,试试这个修改过的代码:

RewriteEngine On

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

RewriteRule ^(.+?)/?$ /user.php?username=$1 [L,QSA]
于 2013-10-30T21:03:53.747 回答