mod_rewrite的 - 如何重定向错误输入“pretty的'网址 [英] mod_rewrite - how to redirect a mistyped 'pretty' url

查看:122
本文介绍了mod_rewrite的 - 如何重定向错误输入“pretty的'网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PHP编写的一个网站,创建'pretty的网址为每个项目(类别和搜索页面上的)这样,

I have a site written in php which creates 'pretty' urls for each item (on category and search pages) like this,

mysite.com/category/slug-for-item-one/1
mysite.com/category/slug-for-item-two/2

在/分类/和/塞/取决于项目的数字ID

The /category/ and /slug/ is dependent upon the numeric id of the item

我有mod_rewrite的服务从URL中的实际内容是这样的:

I have mod_rewrite serve the actual content from urls like this:

mysite.com/view-item.php?id=1
mysite.com/view-item.php?id=2

时,只使用项目ID检索每个项目的内容。

The content for each item is retrieved using just the items id.

下面是我的htaccess的:

Here's my htaccess:

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ view-item.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ view-item.php?pid=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ view-item.php?id=$3 [L]

万物确定,但迄今为止,如果有人在一个url土地上一样,

Everythings ok so far but, if someone lands on on a url like,

mysite.com/1
mysite.com/catey/slug-for-item-one/1 

mysite.com/category/slug-item-one/1

内容仍然是服务,但我怎么能自动复位或重定向到URL的规范版本,为:

the content is still served, but how can I automatically reset or redirect to the canonical version of the url, to:

mysite.com/category/slug-for-item-one/1

我已经搜查SO和谷歌广泛的答案,但没有运气。我只用了mod_rewrite进行简单的重新定位,例如从加www。要以www。我的理解是试探性的。因此我努力理解如何进行的时刻。

I've searched SO and google extensively for an answer, but no luck. I've only used mod_rewrite for simple redirects such as from without www. to with www. and my understanding is tentative thus I'm struggling to understand how to proceed at the moment.

任何人都可以点我在正确的方向?

Can anyone point me in the right direction?

谢谢大家的帮助。许多AP preciated。我对乔恩林的回答实施工作作为我更熟悉如何使用PHP / MySQL的数据库,并了解如何以及为什么它应该工作。我的目标是要在周五完成,完成后会更新这个页面。非常感谢,卡尔。

Thank you everyone for your help. Much appreciated. I'm working on an implementation of Jon Lin's answer as I'm more familiar with using php/mysql databases and understand how and why it should work. I aim to be done by Friday and will update this page when finished. Many thanks, Karl.

*更新* 我已经实现乔恩·林的回答,现在我的'pretty的的URL,当输​​入错误,现在重定向到正确的或规范的网址只是作为SO。谢谢Jon和大家谁贡献!

* UPDATE * I have implemented Jon Lin's answer and now my 'pretty' urls, when mistyped are now redirected to the correct or 'canonical' url just as on SO. Thank you Jon and everyone who contributed!

推荐答案

这可能是你想要在你的视图item.php 来实现,而不是试图使用的东西mod_rewrite的。当你在内部的内容中生成的链接,你可以使用的ID来查找类别和蛞蝓。这将是你通常如何去生成的pretty的搜索引擎友好的环节之一。

This is probably something you want to implement in your view-item.php instead of trying to use mod_rewrite. When you are generating the links internally within your content, you can use the ID's to lookup categories and slugs. This would be how you normally go about generating one of the pretty SEO friendly links.

您首先需要通过类别和蛞蝓到请求。线沿线的东西:

You first need pass the category and slug into the request. Something along the lines of:

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ view-item.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ view-item.php?pid=$2&cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ view-item.php?id=$3&cat=$1&slug=$2 [L]

在你看来,item.php的顶部,只需检查参数存在,当你查找的 ID 他们比较实际的类别和蛞蝓。如果其中一个不匹配(或丢失,如果你想),然后浏览器中使用头重定向到了正确的类别和塞正确的链接()功能:

At the top of your view-item.php, simply check if the cat and slug parameters exist, compare them to the actual category and slug when you do the lookup for the id. If one of them doesn't match (or is missing, if you want), then redirect the browser to the correct link with the correct category and slug using the header() function:

header('HTTP/1.1 301 Moved Permanently');
header("Location: " . $correct_url);
exit();

他们重定向后,重复这个过程,但是这一次,视图item.php认为正确的类别和塞所以页面被送达正常人一样。

After they get redirected, the process repeats itself, but this time, view-item.php sees the correct category and slug so the page gets served like normal.

这篇关于mod_rewrite的 - 如何重定向错误输入“pretty的'网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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