Namtip Posted September 7, 2010 Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212735-httplocalhostusername/ Share on other sites More sharing options...
cags Posted September 7, 2010 Share Posted September 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212735-httplocalhostusername/#findComment-1108164 Share on other sites More sharing options...
Namtip Posted September 7, 2010 Author Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212735-httplocalhostusername/#findComment-1108308 Share on other sites More sharing options...
Namtip Posted September 7, 2010 Author Share Posted September 7, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/212735-httplocalhostusername/#findComment-1108359 Share on other sites More sharing options...
Namtip Posted September 7, 2010 Author Share Posted September 7, 2010 .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. Quote Link to comment https://forums.phpfreaks.com/topic/212735-httplocalhostusername/#findComment-1108382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.