Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

.htaccess - convert original URL to friendly URL

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...