7

In my website there is a link to registered tutors. That link is something like this

http://www.myweb.com/profiles/tutors/index.php?tutorCode=1285&tutorName=Robert%20Sakoo&city=Sidny

So I need to rewrite this link something like this:

www.myweb.com/1285/Rober Sakoo

Can anybody tell me is it possible to rewrite above original URL to my expected URL?

ugnuku
  • 161
  • 1
  • 3
  • 11

2 Answers2

16

You seem to be confused with how URLs and rewriting works. The notion that "I need to rewrite this2 to this1 " means:

  1. Someone either enters this2 in their address bar or clicks on a this2 link.
  2. Server sees a request for this2
  3. Server has a set of rules to internally rewrite the request from this2 to this1
  4. this1 is served to the browser

Note that the important concept in all of this is that the browser requests for the "this2" link, and the server internally rewrites the request to "this1". But this is probably not what you want at all, because then you'd be taking the ugly URLs and rewriting them to the nice looking URLs, sort of missing the point of friendly looking URLs.

Too often, especially around here, people ask for stuff like "I want to change this url to this url", or asking for rewrite when there's a 2 step redirect process. This is the 2nd step (which you are not asking for at all) which takes this1 and redirects the browser to this2 so that the url address bar changes to this2:

  1. Someone either enters this1 in their address bar or clicks on a this1 link.
  2. Server sees a request for this1
  3. Server has a set of rules to externally redirect the browser to this2.
  4. The browser's address bar now says this2
  5. The browser requests this2
  6. Server sees a request for this2
  7. Server has a set of rules to internally rewrite the request from this2 to this1
  8. this1 is served to the browser

So this whole round-about convolution is just so when the browser tries to go to this1, it gets redirected to this2 but still actually gets served the content from this1.

So I assume this must be what you want. Try putting this in your htaccess file:

RewriteEngine On

# check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/tutors/index.php\?tutorCode=([0-9]+)&tutorName=([^&]+)&?([^\ ]+)
# redirect to "this2"
RewriteRule ^profiles/tutors/index\.php /%1/%2/?%3 [R=301,L,NE]

# now rewrite "this2" back to "this1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.+)/$ /profiles/tutors/index.php?tutorCode=$1&tutorName=$2 [L,QSA]

Note that the city parameter is never being encoded in the friendly URL, so it's left as part of the query string. You could change this so that the friendly URL looks like: /id/name/city/ instead of just /id/name/, that modification should be trivial.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1

That would be somewhere near:

RewriteEngine on
RewriteRule ^([0-9]+)/(.+)$ /profiles/tutors/index.php?tutorCode=$1&tutorName=$2 [NC,QSA]

Since you're not specifying what to do about it, i didn't take your city parameter into account, thus added QSA (Query String Append) to the rule so parameters in GET are still passed on to the script.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
darma
  • 4,687
  • 1
  • 24
  • 25
  • i just tested it and it's working, what does your browser show (+http status) when you enter www.lankainstitute.com/1285/Shamdhi in the task url ? – darma Jul 29 '12 at 13:58
  • No it still display original url. Tell me where I need to place this .htaccess file. I just placed it in my root directory and Checked URL. – ugnuku Jul 29 '12 at 14:02
  • 1
    Well it's normal that it still displays the original URL in your browser's task bar, that's what "friendly URLs" are for. But if you place a script physically in /profiles/tutors/index.php then it should be executed. And yes the htaccess must be placed just in your root directory. – darma Jul 29 '12 at 14:07
  • I tried.. But it is not working.. actually where I have gone wrong..?? – ugnuku Jul 29 '12 at 14:28
  • i can only ask again : what does your browser show (+http status) when you enter www.lankainstitute.com/1285/Shamdhi in the url task bar ? – darma Jul 29 '12 at 15:26
  • When I type www.lankainstitute.com/1285/Shamdhi in my browser's address bar I can get this error message 'This page has been accessed in error.' This messege come from my tutor profile index.php page. – ugnuku Jul 29 '12 at 15:37
  • Well if it comes from your /profiles/tutors/index.php script then it IS working, only your script generates an error but the rewrite actually seems to work. – darma Jul 29 '12 at 15:46
  • Why this pretty url not display in browser's address bar?? – ugnuku Jul 29 '12 at 15:52
  • You must be misanderstanding what you're doing here : the "pretty" URL is www.lankainstitute.com/1285/Shamdhi and when you enter it in the address bar it should remain there, we're not talking here about a visible redirect (!!) but about executing a certain script behind the scene. On the other hand, if you try to enter your script's URL directly i.e. www.lankainstitute.com/profiles/tutors/index.php?tutorCode=1285&tutorName=Shamdhi it will obviously just execute this script you requested from the browser. – darma Jul 29 '12 at 16:43
  • Thanks for your comments. Dear darma tell me how can I change visible URL like unvisible scene. – ugnuku Jul 29 '12 at 16:55
  • Change the flags at the end of the rewrite rule to [NC,QSA,R] or [NC,QSA,R=301]. The second will tell the browser that it's a permanent redirection. – Bob Ray Feb 08 '19 at 07:34