如何将此网址重写到重定向网页? [英] How to rewrite this URL to a redirect page?

查看:381
本文介绍了如何将此网址重写到重定向网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在托管服务器(Hostek.com)上使用Microsoft-IIS / 7.5



我有一个现有网站,在Google中有2,820个索引链接。您可以通过搜索Google来查看结果:site:flyingpiston.com大多数页面都使用section,makerid或bikeid来获取正确的信息。大多数链接看起来像这样:

  flyingpiston.com/?BikeID=1068 
flyingpiston.com/?MakerID = 1441
flyingpiston.com/?Section=Maker&MakerID=1441
flyingpiston.com/?Section=Bike&BikeID=1234

在新网站上,我使用.htaccess进行网址重写。新网址如下所示:

  flyingpiston.com/bike/1068/ 
flyingpiston.com/maker/ 1123 /

基本上,我只是想使用我的htaccess文件来引导任何请求,问号在它直接coldfusion页面redirect.cfm。在本页上,我将使用ColdFusion编写自定义301重定向。 ColdFusion的重定向如下:

 < cfheader statuscode =301statustext =Moved Permanently> 
< cfheader name =Locationvalue =http://www.newurl/bike/1233/>
< cfabort>

所以,我的htaccess文件需要看起来像,如果我想推一切带有问号到特定页面?这是我试过,但它不工作。

  RewriteEngine on 
RewriteRule ^? /redirect.cfm [NS,L]

更新。使用下面的建议,我使用这个规则:

  RewriteRule \? /redirect/redirect.cfm [NS,L] 

尝试推送此请求

  http://flyingpiston2012-com.securec37.ezhostingserver.com/?bikeid=1235 

到此页:

  http:// flyingpiston2012 -com.securec37.ezhostingserver.com/redirect/redirect.cfm 


解决方案

有几个原因,你试图不工作。



第一个是RewriteRule使用正则表达式,是一个正则表达式元字符,因此需要使用反斜杠( \?)转义,以使其匹配文字问号字符。



但是,问题的第二部分是正则表达式 RewriteRule 仅针对网址的文件名部分进行测试 - 明确排除查询字符串

为了匹配查询字符串,您需要使用 RewriteCond 指令,位于规则前面(但在 RewriteRule匹配和替换之间应用),作为附加滤波器。有用的位是,您可以指定匹配的URL的哪个部分(以及具有使用非正则表达式测试的选项)。



承载所有这一切

  RewriteCond%

使用查询字符串匹配/重写请求的最简单方法是< {QUERY_STRING}。
RewriteRule。* /redirect/redirect.cfm

{QUERY_STRING} 是对正则表达式进行测试的对象(CF的CGI范围中的所有内容都可以在这里使用,以及其他一些东西 - 参见文档中的服务器变量框)。



c $ c>。只是说确保匹配的项目有任何单个字符



查询字符串 - 如果要舍弃它,可以在替换URL的末尾放置一个。 (如果您需要在网址上使用查询字符串,并且不要丢弃旧版本,请使用 [QSA] 标记。)



在相反的方向,你丢失了URL的文件名部分 - 为了保留这一点,你可能想将它附加到替换为PATH_INFO,使用自动整个匹配捕获 $ 0



这两个东西一起提供:

  RewriteCond%{QUERY_STRING}。 
RewriteRule。* /redirect/redirect.cfm/$0?

最后一件事是你要防范无限循环 - 字符串,所以它总是失败的RewriteCond,但更好是安全的(特别是如果你可能需要添加一个查询字符串),你可以做一个额外的RewriteCond:

  RewriteCond%{QUERY_STRING}。 
RewriteCond%{REQUEST_URI}!/redirect/redirect\.cfm
RewriteRule。* /redirect/redirect.cfm/$0?

多个RewriteCond组合为ANDs,

当然,您可以添加任何标记到RewriteRule,使其具有所需的行为。


I am using Microsoft-IIS/7.5 on a hosted server (Hostek.com)

I have an existing site with 2,820 indexed links in Google. You can see the results by searching Google with this: site:flyingpiston.com Most of the pages use a section, makerid, or bikeid to get the right information. Most of the links look like this:

flyingpiston.com/?BikeID=1068
flyingpiston.com/?MakerID=1441
flyingpiston.com/?Section=Maker&MakerID=1441
flyingpiston.com/?Section=Bike&BikeID=1234

On the new site, I am doing URL rewriting using .htaccess. The new URLs will look like this:

flyingpiston.com/bike/1068/
flyingpiston.com/maker/1123/

Basically, I just want to use my htaccess file to direct any request with a "?" question mark in it directly a coldfusion page called redirect.cfm. On this page, I will use ColdFusion to write a custom 301 redirect. Here's what ColdFusion's redirect looks like:

<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="http://www.newurl/bike/1233/">
<cfabort>

So, what does my htaccess file need to look like if I want to push everything with a question mark to a particular page? Here's what I have tried, but it's not working.

RewriteEngine on
RewriteRule ^? /redirect.cfm [NS,L]

Update. Using the advice from below, I am using this rule:

RewriteRule \? /redirect/redirect.cfm [NS,L]

To try to push this request

http://flyingpiston2012-com.securec37.ezhostingserver.com/?bikeid=1235

To this page:

http://flyingpiston2012-com.securec37.ezhostingserver.com/redirect/redirect.cfm

解决方案

There's a couple of reasons what you're trying isn't working.

The first one is that RewriteRule uses a regex, and ? is a regex metacharacter, which therefore needs be escaped with a backslash (\?) to tell it to match the literal question mark character.

However, the second part of the problem is that the regex for RewriteRule is only tested against the filename part of the URL - it specifically excludes the query string.

In order to match against the query string you need to use the RewriteCond directive, placed on the line before the rule (but applied in between the RewriteRule matching and replacing), acting as an additional filter. The useful bit is that you can specify which part of the URL to match against (as well as having the option for using non-regex tests).

Bearing all this in mind, the simplest way to match/rewrite a request with a query string is:

RewriteCond %{QUERY_STRING} .
RewriteRule .* /redirect/redirect.cfm

The %{QUERY_STRING} is what the regex is tested against (everything in CF's CGI scope can be used here, and some other stuff too - see the Server Variables box in the docs).

The single . just says "make sure the matched item has any single character"

At the moment, this rule will preserve the existing query string - if you want to discard it, you can place a ? onto the end of the replacement URL. (If you need to use a query string on the URL and not discard the old version, use the [QSA] flag.)

In the opposite direction, you're losing the filename part of the URL - to preserve this, you probably want to append it onto the replacement as PATH_INFO, using the automatic whole-match capture $0.

These two things together provides:

RewriteCond %{QUERY_STRING} .
RewriteRule .* /redirect/redirect.cfm/$0?

One final thing is that you'll want to guard against infinite loops - the above rule strips the query string so it will always fail the RewriteCond, but better to be safe (especially if you might need to add a query string), which you can do with an extra RewriteCond:

RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !/redirect/redirect\.cfm
RewriteRule .* /redirect/redirect.cfm/$0?

Multiple RewriteCond are combined as ANDs, and the ! negates the match.

You can of course add whatever flags are required to the RewriteRule to have it behave as desired.

这篇关于如何将此网址重写到重定向网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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