Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert original URL to friendly URL

Tags:

.htaccess

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?

like image 413
ugnuku Avatar asked Jul 29 '12 13:07

ugnuku


People also ask

What is SEO URL?

What are SEO friendly URLs? SEO friendly URLs are URLs that are designed to meet the needs of users and searchers. Specifically, URLs optimized for SEO tend to be short and keyword-rich.


2 Answers

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.

like image 166
Jon Lin Avatar answered Oct 03 '22 03:10

Jon Lin


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.

like image 27
darma Avatar answered Oct 03 '22 03:10

darma