Jump to content

Recommended Posts

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 by adi123
Link to comment
https://forums.phpfreaks.com/topic/270602-php-rewrite-url/
Share on other sites

/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 by requinix
Link to comment
https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1392411
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1393996
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/270602-php-rewrite-url/#findComment-1396088
Share on other sites

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.