理解重定向和重写 .htaccess 之间的区别 [英] Understanding difference between redirect and rewrite .htaccess

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

问题描述

我想了解使用 .htaccess 重定向和重写 URL 之间的区别.所以这是一个例子:假设我有一个像 www.abc.com/index.php?page=product_types&cat=88 的链接(称之为原始"网址)

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

这段代码没有任何问题,它可以解决问题.但是,如果我在地址栏中输入原始 url,内容会出现,但 url 不会更改.所以它仍然是 www.abc.com/index.php?page=product_types&cat=88

Nothing wrong with this code and it does the trick. However, if I type in the original url in the address bar, the content comes up, but the url does not change. So it remains as www.abc.com/index.php?page=product_types&cat=88

但是,如果我输入 www.abc.com/index.php?page=product_types& 的话,如果我想在地址栏中显示所需的 url (/shoes) 该怎么办?猫=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?

推荐答案

一些解释可以在这里找到:https://stackoverflow.com/a/11711948/851273

Some of the explanation can be found here: https://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.地址栏中的 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 绝对没有任何作用.如果 google 或 reddit 或任何网站有 www.abc.com/index.php?page=product_types&cat=88 的链接,您的内部服务器重写规则对此完全没有作用,对任何人也没有作用谁点击了该链接,或者任何出于任何原因碰巧请求该 URL 的客户.重写规则所做的只是在服务器内部将包含 shoes 的内容更改为 /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]

这应该仅用于使其中的链接指向正确的位置.您必须确保您的内容生成正确的链接.这意味着您网站上的所有内容都使用 /shoes 链接,而不是 /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.

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

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