adi123 Posted November 12, 2012 Share Posted November 12, 2012 (edited) I am trying to change the url for my users on my website. The link is like this: www.mysite.co.uk/user.php?u_id=1245&username=ady I would like the link to be read as: www.mysite.co.uk/ady The code is below: RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([a-zA-Z0-9]+)\/?$ user.php?username=$1 I get an error on the page when using this code. Can someone please help. Can't figure out the error. Edited November 12, 2012 by adi123 Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/ Share on other sites More sharing options...
requinix Posted November 12, 2012 Share Posted November 12, 2012 (edited) I can't figure out the error either. Probably because you didn't tell us what it is. Edited November 12, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1391863 Share on other sites More sharing options...
adi123 Posted November 14, 2012 Author Share Posted November 14, 2012 the error is that the page does not exist. The url only works when you type "www.mysite.co.uk/1245ady" but I want the url to work when you type "www.mysite.co.uk/ady". Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1392362 Share on other sites More sharing options...
requinix Posted November 14, 2012 Share Posted November 14, 2012 (edited) /1245ady works? I'm surprised. Maybe it's looking up the user account based on the integer value of the username? Also I think when you say "page does not exist" you mean to say "user does not exist". Big difference there. Your original URL /user.php?u_id=1245&username=ady doesn't match up with what you're rewriting to /user.php?username=ady You lost the u_id. Two options: 1. Include the u_id in the friendly URL so that you can pass it to user.php in the rewriting 2. Alter user.php so that it doesn't require the u_id and can find users based on the username alone Edited November 14, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1392411 Share on other sites More sharing options...
jbrennan Posted November 21, 2012 Share Posted November 21, 2012 This should get you the URL structure you are looking for. RewriteEngine On RewriteCond %{REQUEST_URI} ^/user\.php RewriteCond %{QUERY_STRING} username=([a-zA-Z0-9]*) RewriteRule ^(.*)$ http://www.test.com/%1? [L The url only works when you type "www.mysite.co.uk/1245ady" Based on that statement, I am going to guess you are using Joomla. If so, Joomla inserts the row id in the URL string for it's SEF urls. It then retrieves and uses that to return the appropriate content for the page. Changing only this id in the url will return a different row from the database, even without changing the rest of the URL. You will likely have to change a sql query to use the "username" column rather than the "U_id" column to lookup the users information for the page. If you not using Joomla, what platform are you using. Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1393996 Share on other sites More sharing options...
paulinetaylor85 Posted November 29, 2012 Share Posted November 29, 2012 I think you should follow the below steps : » mod_rewrite module must be loaded in apache server. » Need to enable FollowSymLinks option otherwise you may encounter 500 Internal Server Error. » After that you need to create a .htaccess file in the root folder of your web directory. Quote Link to comment https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1396088 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.