user2935089
user2935089

Reputation: 23

Trying to create clean URLs but getting some errors

I'm trying to create beauty url from htaccess this is my code

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

I want to display user data like this but problem is when i run anyother file like domain.com/contactus this redirects to user.php?username=contactus Also on this expression [a-zA-Z0-9_] i want to put (.) Dot because there are usernames like john.dyer to is this expression correct - [a-zA-Z0-9_.]

this is my 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';
}
?>

Sorry I'm very new to this..I'm learning this things so please tell me if there is any better way to do this. Thanks

Upvotes: 2

Views: 72

Answers (1)

anubhava
anubhava

Reputation: 785128

You have a quite a few problem, try this modified code:

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]

Upvotes: 1

Related Questions