Jump to content

Recommended Posts

http://mirificampress.com/permalink/beyond_redirect_using_rewriterules_in_htaccess

 

I've checked out a couple of tutorials on mod_rewrite including the one above. But I'm still not grasping it and I'm running out of tutorials! I'm smart!

 

I want to clean my urls to give a user his/her own page. e.g http://localhost/username

 

So I've put all of my php pages into a pages file within the htdocs folder instead of the pages in the htdocs folder its self. The only file in there now is the .htaccess (I'm not sure if I should move that as well?)  file that I've put the following code it it.

Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteRule ^(.{6,12})$ http://localhost/pages/users/index.php?user_name=$1 [NC]
RewriteRule ^$ http://localhost/pages/index.php [R=301,NC] 

 

user_name.php - userprofile

<?php
include 'db.inc.php';

$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or 
    die ('Unable to connect. Check your connection parameters.');

mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
$query = 'SELECT
        u.name, bio, info
        FROM
        user u JOIN profile i ON u.name = i.name
        WHERE
        name = "' . mysql_real_escape_string($_GET['name'], $db) . '"';
    $result = mysql_query($query, $db) or die(mysql_error());


?>

<html>
   <head>
      <title> <?php $_GET['name']; ?> </title>
   </head>
   <body>
      <p>
         The requested page was:
<?php
echo $name; 
echo $bio;
echo $exhib;

?>
      </p>
   </body>
</html>

 

What am I doing wrong? When I type http://localhost/testname into my browser I get "object isn't found. Error 404." The user name is already in the database.

I'm sorry, I know you must look at my attempt and think I should give up php. If you know of any idiot proof tutorials or could help my code, I'd really appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/212735-httplocalhostusername/
Share on other sites

What you have looks correct to me. A 404 error in this situation is normally because the RewriteRule doesn't match your URI or because the substitution pattern doesn't match an actual file. Since in your case it seems that the first pattern matches, I would assume the second doesn't. Change the following...

 

RewriteRule ^(.{6,12})$ /pages/users/index.php?user_name=$1

 

If you still get the error then try the following as a debug technique...

 

RewriteRule ^(.{6,12})$ http://www.google.com/search?q=$1 [NC]

 

If you end up at google with 'username' as a search term then http://localhost/pages/users/index.php?user_name=$1

 

 

I tried both of your rewrites, same "object not found" error. I've used your debug technique, and I've ended up at google with username in the search directory. What does that mean? Could one of my files be in the wrong directory? Does the .htaccess file have to stay in the htdocs folder or can I move it? I need to get some food brb.

I've found out you need two .htaccess files (didn't know you could have more than one!) One in the 'htdocs' folder and one in the 'pages' folder. Apparently the second .htaccess file is used to block the rewrite rules of the first one.

Still having problems, but I'm posting this in case it might help another reader. I will make this work!

.htaccess in the pages folder

Options +FollowSymlinks
RewriteEngine off

 

.htaccess in the htdocs folder

Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10

RewriteRule ^(.{6,12})$ http://mydomain.com/pages/users/index.php?user_name=$1 [NC]
RewriteRule ^$ http://mydomain.com/pages/index.php [R=301,NC] 

 

After sorting the htaccess folders out and fiddling I've sorted it out. Thanks guys.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.