将原始 URL 转换为友好 URL [英] convert original URL to friendly URL

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

问题描述

在我的网站上有一个指向注册导师的链接.那个链接是这样的

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

所以我需要像这样重写这个链接:

www.myweb.com/1285/Rober Sakoo

谁能告诉我是否可以将上面的原始 URL 重写为我期望的 URL?

解决方案

您似乎对 URL 和重写的工作方式感到困惑.我需要将 this2 重写为 this1" 的概念意味着:

  1. 有人在地址栏中输入this2或点击this2链接.
  2. 服务器看到对 this2
  3. 的请求
  4. 服务器有一组规则来在内部将请求从 this2 重写为 this1
  5. this1 提供给浏览器

请注意,所有这些中的重要概念是浏览器请求this2"链接,并且服务器在内部将请求重写为this1".但这可能根本不是您想要的,因为那样您就会将丑陋的 URL 重写为漂亮的 URL,有点忽略了看起来友好的 URL.

经常,尤其是在这附近,人们要求诸如我想将此 url 更改为此 url"之类的内容,或者在有两步重定向过程时要求重写.这是第 2 步(您根本不需要),它使用 this1将浏览器重定向到 this2 以便 url 地址栏更改为 this2:

  1. 有人在地址栏中输入this1或点击this1链接.
  2. 服务器看到对 this1
  3. 的请求
  4. 服务器有一组规则来将浏览器从外部重定向this2.
  5. 浏览器的地址栏现在显示 this2
  6. 浏览器请求this2
  7. 服务器看到对 this2
  8. 的请求
  9. 服务器有一组规则来在内部将请求从 this2 重写为 this1
  10. this1 提供给浏览器

所以整个循环卷积只是,所以当浏览器尝试去this1时,它被重定向到this2但仍然实际上是从 this1 获得内容.

所以我认为这一定是您想要的.尝试将其放入您的 htaccess 文件中:

RewriteEngine On# 检查是否为this1"的实际请求RewriteCond %{THE_REQUEST} ^[AZ]{3,9}/profiles/tutors/index.php?tutorCode=([0-9]+)&tutorName=([^&]+)&?([^]+)# 重定向到this2"重写规则 ^profiles/tutors/index.php/%1/%2/?%3 [R=301,L,NE]# 现在将this2"重写回this1"RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^([0-9]+)/(.+)/$/profiles/tutors/index.php?tutorCode=$1&tutorName=$2 [L,QSA]

请注意,city 参数永远不会在友好 URL 中编码,因此它作为查询字符串的一部分保留.您可以更改此设置,使友好 URL 看起来像:/id/name/city/ 而不仅仅是 /id/name/,该修改应该是微不足道的.>

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?

解决方案

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.

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

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