重写URL并在位置栏中显示新的URL [英] Rewriting URL and making new URL show in location bar

查看:113
本文介绍了重写URL并在位置栏中显示新的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码重写某些URL:

I'm using the below code to rewrite some URL's:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^New_Hampshire/[a-zA-Z0-9_+\-\s]*\.html$ NH/

请记住,在此下方也有此规则.

Keep in mind that there is also this rule below that..

RewriteRule ^([a-zA-Z]{2})/ state.php?state=$1 [L]

现在,我要实现的目标是,例如有人去

Now, what I am wanting to achieve is if someone goes to for example:

www.mydomain.com/New_Hampshire/index.html

我希望位置栏显示:

www.mydomain.com/NH/

通常,我可以通过在末尾使用[R]标志来实现此目的,但是在这种情况下,它不起作用,我想这可能与第二条规则有关.

Usually I am able to achieve this by using the [R] flag at the end, however in this instance it doesn't work, which I am guessing it may have something to do with the secondary rule.

这可能吗?

此外,我该如何更改它,以便它也可以重定向:

Also, how can I change this so that it also redirects:

www.mydomain.com/New_Hampshire/

如您所见,上面没有包含index.html参考.

As you can see the above doesn't contain an index.html reference.

最后,如果我想进行这些301重定向,我是否会做类似的事情.

Lastly, if I wanted to make these 301 redirects, would I just do something like..

RewriteRule ^New_Hampshire/[a-zA-Z0-9_+\-\s]*\.html$ NH/ [R=301]

推荐答案

您需要了解URL重写(在您的情况下是通过Apache的mod_rewrite实现)与重定向之间的区别,重定向是HTTP规范的一部分,通过发送客户端(浏览器)的标准HTTP标头和状态代码.

You need to understand the difference beween URL rewrite (achieved in your case by Apache's mod_rewrite) and redirect, which is part of the HTTP spec and achieved by sending a standard HTTP header and status code to the client (browser).

使用URL重写,当Web服务器处理请求时,您可以选择将客户端发送的请求URL映射到由其他本地路径表示的本地资源.这样做是出于各种原因,例如能够向用户显示干净且简短的URL,隐藏服务器上资源的内部结构,保护某些资源等.

With URL rewrite, when a request is processed by the web server, you have an option to map a requested URL sent by the client, to a local resource represented by a different local path. This is done for various reasons like being able to present clean and short URLs to a user, hide the internal structure of the resources on the server, protecting certain resources etc.

浏览器通常不知道服务器重写了URL(除非有其他指示),并且仍将在地址栏中显示原始请求的URL.无法在客户端不知道的情况下远程操作客户端请求的URL.

The browser usually doesn't know a URL was re-written by the server (unless there is some other indication) and will still display the original requested URL in the address bar. It is not possible to manipulate the client's requested URL remotely without the client knowing about it.

HTTP重定向是HTTP规范的一部分,是一种告诉客户端可以在其他位置找到请求的URL的方法. Web服务器通过发送带有重定向的URL和HTTP状态代码(告诉客户端发生了什么情况)的"Location"标头来实现此目的.为此有几种30x状态代码,最常用的是301(告诉客户端该资源已永久移动,此后应使用新的URL寻址)和302告诉浏览器该资源已找到但是临时的驻留在新的URL下(在规则中指定R标志时,Apache的实现默认情况下使用302).

HTTP redirect, on the other hand is part of the HTTP spec and is a way to tell the client that the requested URL can be found in a different location. The web server does this by sending a "Location" header with the redirected URL and an HTTP status code that tells the client what happened. There are several 30x status codes for this, with the most commonly used are 301 for telling the client that the resource was permanently moved and should be addressed by the new URL from now on and 302 for telling the browser that the resource was found but temporary resides under a new URL (Apache's implementation uses 302 by default when you specify the R flag in the rule).

客户端将收到带有新位置和状态代码的HTTP响应,并向新URL发出新的HTTP请求.如果这是主要请求(即不是对图像或脚本之类资源的子请求),它将替换地址栏中的位置.

The client will receive an HTTP response with the new location and the status code and issue a new HTTP request to the new URL. It will also replace the location in the address bar if this is the main request (i.e. not a sub request to a resource like an image or script).

请参见 http://httpd.apache.org/docs/current /rewrite/flags.html#flag_r

请注意,Apache的文档建议将L标志与重定向标志一起使用(例如[R,L]或[R = 301,L]),否则您会得到意想不到的结果(因为它将继续处理以下规则) .这可能就是为什么您的R标志不起作用的原因.

Note that Apache's documentation advises to use the L flag with the redirection flag (e.g. [R, L] or [R=301, L]) or you can get unexpected results (because it will continue to process the following rules). This is probably why your R flag doesn't work.

因此,您可以执行以下操作(仅作为示例):

So, you can do something like this (just an example):

RewriteRule ^New_Hampshire.* NH/ [R=301, L]

这会将客户端从任何/New_Hampshire前缀(带有或不带有* .html)URL重定向到NH/,并更改地址栏.

This will redirect the client from any /New_Hampshire prefix (with or without the *.html) URL, to NH/ and change the address bar.

然后下一条规则:

RewriteRule ^NH/? state.php?state=NH [L]

或更通用的:

RewriteRule ^([a-zA-Z]{2})/? state.php?state=$1 [L]

这将捕获NH(或其他状态)URL,并将其重写为适当的实际资源(state.php).

This will catch the NH (or other state) URL and rewrite it to the proper actual resource (state.php).

,这只是一个向您展示规则的示例.您可能需要处理第一条规则中的逻辑.因为有些事情需要将全州名称(New_Hampshire)映射到快捷方式(NH),除非您要为每个州编写规则.

BUT, this is just an example to show you the rules. You probably need to handle the logic in the first rule. Because something needs to map the full states' names (New_Hampshire) to the shortcuts (NH), unless you want to write a rule for each state.

这篇关于重写URL并在位置栏中显示新的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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