mod-rewrite自动更新网址 [英] Mod Rewrite automatically update URL

查看:238
本文介绍了mod-rewrite自动更新网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我有一个.htaccess文件与mod-rewrite,所以我的网址都有点更好看,更搜索引擎友好的。

I have a website where I have a .htaccess file with mod rewrite, so my URL's are a bit nicer to look at and more SEO friendly.

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

ErrorDocument 404 /index.php?error=404

RewriteRule ^(admin)($|/) - [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_]+)\.php$ index.php?country=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_A-Яа-я-]+)/([a-zA-Z0-9_-]+)\.php$ index.php?country=$1&id=$3
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)/(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\.php$ index.php?country=$1&subid=$5&id=$4
RewriteRule ^.+?---(.+)$ images/$1 [L,NE]
Rewriterule ^sitemap.xml$ sitemap.php [L]

正如你可以在上面的规则看,有通配符,这意味着两者之间的某个向前的斜线,任何事情都会发生。我用它,方式是语言选择和命名的标题的页面,与ID变量结束控制,以显示其在数据库表中的行。例如:

As you can see in the above rules, there are "wildcards" meaning that in between some of the forward slashes, anything goes. The way I use it, is for language selection and naming the title to the page, ending of with the id variables to control which row in the table in the database to show. example:

http://www.domain.com/en/加热器/热加热器/ 26 / 60.php

以上URL包含域/的langugage /通配符/通配符/ id变量/ id变量

The above URL contains the domain / the langugage / wildcard / wildcard / id variable / id variable

现在的问题是,当页面上的链接得到由标题更新(让我们说,热加热器更名为热红加热器)由谷歌索引的网址是不一样的,并且两个网址仍然工作。

Now the problem is, when the links on the page gets updated by title (let's say that the hot-heater changes name to hot-red-heater) the URL indexed by Google is not the same, and both URL's still work.

我想知道如何mod-rewrite可用于自动更新通配符来正确的标题。就像这里的StackOverflow,网址为在URL这个问题的标题 - 如果我改变这种在URL中,URL自动改变这一回原题。这是怎么做的?

I would like to know how mod rewrite can be used to automatically update the wildcards to the correct title. Just like here on Stackoverflow, the URL has the title of this question in the URL - if I alter this in the URL, the URL automatically changes this back to the original title. How is this done?

非常感谢提前。

推荐答案

如果你需要有这样的网址#1相同的行为,那么你需要有一些服务器端支持(如PHP)为好。请考虑以下code片断:

If you need to have same behavior like Stackoverflow URLs then you need to have some server side support (e.g. PHP) as well. Consider following code snippet:

的.htaccess:

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?id=$1&title=$2 [L,QSA]

的index.php:

<?php
   $id    = $_GET['id'];
   $title = $_GET['title'];

   $dbTitle = // get title by doing a database query using $id
   // ...

   if ($title != $dbTitle) {
      // redirect with 301 to correct /<id>/<title> page
      header ('HTTP/1.1 301 Moved Permanently');
      header('Location: /' . $id . '/' . $dbTitle);
      exit;
   }
   // rest of your script
?>

这会支持这样一个URL,这样即 / ID /一些标题

This will support a URL like SO i.e. /id/some-title

这篇关于mod-rewrite自动更新网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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