转换原始网址友好的URL [英] convert original URL to friendly URL

查看:473
本文介绍了转换原始网址友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上有一个链接注册的导师。这种联系是这样的

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

<$c$c>http://www.myweb.com/profiles/tutors/index.php?tutor$c$c=1285&tutorName=Robert%20Sakoo&city=Sidny

所以,我需要重写这个链接是这样的:

So I need to rewrite this link something like this:

www.myweb.com/1285/Rober Sakoo

谁能告诉我是有可能重写上面的原始URL我的预期网址是什么?

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

推荐答案

您似乎混淆了如何URL和改写的作品。是的的概念:我需要重写 this2 this1 的意思是:

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

  1. 有人要么进入 this2 在一个 this2 链接的地址栏或点击。
  2. 服务器看到了 this2
  3. 的请求
  4. 服务器都有一套规则,内部重写从请求 this2 this1
  5. this1 供应到浏览器
  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

请注意,在所有这些重要的概念是,作为this2链接,然后在浏览器请求的服务器内部重写请求this1。但是,这可能不是你想要的所有东西,因为那样的话你会采取丑陋的网址,并将其改写到好看的网址,有点缺少友好寻找网址的地步。

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.

很多时候,尤其是在这里,人们问的东西,如我想这个网址更改为这个URL,或者要求重写时,有一个2步重定向过程。这是一个发生在第2步(你是不是要求在所有)的 this1 将浏览器重定向的为 this2 这样的URL地址酒吧修改 this2

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. 有人要么进入 this1 在一个 this1 链接的地址栏或点击。
  2. 服务器看到了 this1
  3. 的请求
  4. 服务器都有一套规则的外部重定向浏览器的为 this2
  5. 在浏览器的地址栏中说,现在的 this2
  6. 在浏览器请求的 this2
  7. 服务器看到了 this2
  8. 的请求
  9. 服务器都有一套规则,内部重写从请求 this2 this1
  10. this1 供应到浏览器
  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

所以,这整个迂回的卷积的只是的,所以当浏览器试图转到 this1 ,它就会被重定向到 this2 ,但仍实际上得到服务的内容 this1

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.

所以,我想这一定是你想要的。尝试在你的htaccess文件把这样的:

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]

注意城市参数永不EN codeD在友好的URL,所以剩下的作为查询字符串的一部分。你可以改变这一点,以便在友好的URL看起来像: / ID /姓名/城市/ ,而不是仅仅 / ID /姓名/ ,该修改应该是微不足道的。

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.

这篇关于转换原始网址友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆