2013-10-30 15 views
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的這個表達[a-zA-Z0-9_]我想提出點,因爲有像john.dyer用戶名的就是這個表達式正確的用戶名=聯繫我們() - [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'; 
} 
?> 

對不起,我對此很新。我正在學習這些東西,所以請告訴我是否有更好的方法來做到這一點。由於

回答

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] 
+0

工作不錯,但還是我得到律誤差在這個表達式與像如果我給的用戶名= somthing.something(。) .123它說沒有找到,但用戶名是他們的數據庫,並感謝您的幫助 – user2935089

+0

好吧現在檢查編輯的答案。 – anubhava

+0

還沒有工作somthing.123說沒有找到 – user2935089