如何从旧的PHP脚本重定向到一个新的PHP脚本使用mod_rewrite [英] How to redirect from an old php script to a new php script using mod_rewrite

查看:141
本文介绍了如何从旧的PHP脚本重定向到一个新的PHP脚本使用mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里的要求,希望我不会不高兴的人。

It's my first request here and hopefully I won't upset anyone.

下面是我的故事:

我已经找遍了这个地方的解决方案,是不是能够找到一个。这里的希望有人可以给些意见。

I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.

我已经基本上成功地使用Apache的的mod_rewrite 创建搜索引擎友好的URL,我的网站。

I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.

例如。老路的 www.hostname / index1.php?会话=用户的现改写为 www.hostname /用户的,这会导致成搜索引擎友好的URL地址。

E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.

然而,旧的路径仍然有效。我需要以某种方式重定向所有传入的旧的 index1.php 的请求到新网址,搜索引擎优化友好的,对于搜索引擎的链接倍受欢迎转移到新的。我相信我可以有一个无限循环重定向,这就是为什么它不工作。

However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.

我的code到目前为止:

My code so far:

Options +FollowSymLinks
RewriteEngine On
RewriteBase / 

## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]

## Catch the above and rewrite the URL 
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]

以上规则从来没有的打时的的htaccess 的文件进行分析的。

The above rules never get hit when the htaccess file is parsed.

它击中了我,我会在这里做某种形式的重定向循环的,所以我认为有关重命名 index1.php 文件为 index2.php 并创建是这样的:

It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:

## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]

## Catch the above and rewrite the URL 
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]

然而,失败太

However, that failed too.

什么是这个最好的方法?我在做什么错在这里?

What would be the best approach to this? What am I doing wrong here?

感谢您!

推荐答案

您不能对查询字符串匹配(一切后,)的重写规则,所以你不能对届= 部分匹配。您也可以对%{QUERY_STRING}不是简单地匹配变种,因为被你所填充时,它重写搜索引擎友好的URL,以一个与查询字符串的其他规则。所以,你需要匹配实际的请求:

You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]

这篇关于如何从旧的PHP脚本重定向到一个新的PHP脚本使用mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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