convert original URL to friendly URL

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.

Leave a Comment