重定向和重写.htacess之间的理解差异 [英] Understanding difference between redirect and rewrite .htacess

查看:172
本文介绍了重定向和重写.htacess之间的理解差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解重定向和使用的.htaccess重写URL之间的差异。 因此,这里有一个例子:说我有像 www.abc.com/的index.php链接页面= product_types和放大器;猫= 88 (称之为原始的URL)

I'd like to understand the difference between redirecting and rewriting a URL using .htaccess. So here's an example: Say I have a link like www.abc.com/ index.php?page=product_types&cat=88 (call this the "original" url)

但是当 abc.com/shoes 用户类型(我们称之为理想的网址),他们需要看到上面的链接的内容。要做到这一点,我会做这样的:

But when the user types in abc.com/shoes (let's call this the "desired" url), they need to see the contents of the above link. To accomplish this, I would do this:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)shoes(.*)$ index.php?page=product_types&cat=88

没有错code和它的伎俩。但是,如果我输入的地址栏中的原始地址,内容出现,但是URL不会改变。因此,它仍然是 www.abc.com/index.php?page=product_types&cat=88

但是,如果我想所需的URL( /鞋),以显示在地址栏,如果我输入 www.abc.com ?/ index.php页面= product_types和放大器;猫= 88 ?这将如何使用的.htaccess实现的呢?我正在运行到一个潜在的循环?

But what if I wanted the desired url (/shoes) to show up in the address bar if I typed in www.abc.com/ index.php?page=product_types&cat=88? How would this be accomplished using .htaccess? Am I running into a potential loop?

推荐答案

部分的解释,可以在这里找到:的http:// stackoverflow.com/a/11711948/851273

Some of the explanation can be found here: http://stackoverflow.com/a/11711948/851273

的要点是,重写只发生在服务器上,客户端(浏览器)是视而不见。该浏览器发送请求并得到的内容,这是毫无收获到所发生的服务器上,以服务请求。

The gist is that a rewrite happens solely on the server, the client (browser) is blind to it. The browser sends a request and gets content, it is none the wiser to what happened on the server in order to serve the request.

一个重定向是服务器响应,以一个请求,告诉客户端(浏览器)提交新的请求。浏览器请求一个URL,这个网址是什么在地址栏中,服务器收到该请求和响应重定向,浏览器获得响应并加载URL中的服务器的响应。在地址栏中的URL是现在新的网址,浏览器发送新网址的请求。

A redirect is a server response to a request, that tells the client (browser) to submit a new request. The browser asks for a url, this url is what's in the location bar, the server gets that request and responds with a redirect, the browser gets the response and loads the URL in the server's response. The URL in the location bar is now the new URL and the browser sends a request for the new URL.

只需在内部服务器上的重写也绝对没有什么在野外的URL。如果谷歌或reddit的或任何网站有链接 www.abc.com/index.php?page=product_types&cat=88 ,内部服务器的重写规则做绝对没问题到,也没有给任何人谁点击了该链接,或出现这种情况,请求URL以任何理由任何客户端。所有重写规则的作用是内部改变的东西,它包含 /index.php?page=product_types&cat=88 服务器内。

Simply rewriting internally on the server does absolutely nothing to URLs in the wild. If google or reddit or whatever site has a link to www.abc.com/index.php?page=product_types&cat=88, your internal server rewrite rule does absolutely nothing to that, nor to anyone who clicks on that link, or any client that happens to request that URL for any reason whatsoever. All the rewrite rule does is internally change something that contains shoes to /index.php?page=product_types&cat=88 within the server.

如果你想要让这样的请求的index.php页面做所有的查询字符串,你可以告诉客户端(浏览器)的重定向的更好看的URL。你必须要小心,因为重写规则循环,您重定向将在内部重写,这将导致一个重定向,将在内部重写等。引起循环,将抛出一个500服务器错误。所以,你可以专门匹配的要求本身:

If you want make it so a request is made for the index.php page with all of the query strings, you can tell the client (browser) to redirect to the nicer looking URL. You need to be careful because rewrite rules loop and your redirect will be internally rewritten which will cause a redirect which will be internally rewritten, etc.. causing a loop and will throw a 500 Server Error. So you can match specifically to the request itself:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=product_types&cat=88
RewriteRule ^/?index.php$ /shoes [L,R=301]

这应该只被用来制造它,以便在野外链接指向正确的地方。 您必须确保您的内容产生了正确的链接。这意味着一切都在你的网站使用的是 /鞋链接,而不是 /index.php?page=product_types&cat=88 链接。

This should only be used to make it so links in the wild get pointed to the right place. You must ensure that your content is generating the correct links. That means everything on your site is using the /shoes link instead of the /index.php?page=product_types&cat=88 link.

这篇关于重定向和重写.htacess之间的理解差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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