URL重写高见 [英] URL rewriting advice please

查看:165
本文介绍了URL重写高见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出我的网址搜索引擎更友好,例如更改此:

<一个href=\"http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce\" rel=\"nofollow\">http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce

要像这样漂亮的:

  http://www.chillisource.co.uk/product/Daves_Gourmet/Daves_Insanity_Sauce

什么是打算这样做最好的方法?我有一个看的htaccess文件这样做,但这个好像很复杂。

在此先感谢


解决方案

本·佩顿,其实也有一个非常讨巧。像Word preSS CMSes倾向于使用它而不是瞎搞与常规的前pressions的。


的.htaccess

首先,您使用的.htacess与下面的​​内容:

 &LT; IfModule mod_rewrite.c&GT;
  RewriteEngine叙述在
  的RewriteBase /
  的RewriteCond%{} REQUEST_FILENAME!-f
  的RewriteCond%{} REQUEST_FILENAME!-d
  重写规则。的index.php [L]
&LT; / IfModule&GT;

让我来解释一下它(逐行):


  • 如果使用Apache模块命名mod_rewrite的存在..

    • 打开模块

    • 让人们知道,我们将改写后的任何首发
      域名(仅改写某些目录,使用的RewriteBase
      /子目录/

    • 如果请求的路径不存在作为文件...

    • 和它甚至不存在,作为一个目录...

    • 重定向请求到index.php文件


  • 关闭我们的模块状态

以上只是一个简单说明。你不是真的需要它来使用它。

我们所做的,就是我们说的Apache,这将最终成为404的所有请求将它们传递给index.php文件,在这里我们可以手动处理请求。


PHP端

在PHP端,里面的的index.php ,你只需要解析的原始URL。此URL在 $ _ SERVER 变量传递为 $ _ SERVER ['REDIRECT_URL']

最好的部分,如果没有重定向,这个变量没有设置!

所以,我们的code会落得像:

 如果(使用isset($ _ SERVER ['REDIRECT_URL'])){    $ URL =爆炸('/',$ _ SERVER ['REDIRECT_URL']);    开关($网址[0]){
        情况下的家://如:/家庭/
            打破;
        案件关于://如:/约/
            打破;
        案形象://如:/图像/
            开关($网址[1]){
                案例'2010'://如:/图像/ 2010 /
                    打破;
                案例'2011'://如:/图像/ 2011 /
                    打破;
            }
            打破;
    }}


易于集成

我几乎忘记提到这一点,但是,由于它的工作方式,你甚至可以最终都不会改变现有的code!

少说话,更多的例子。比方说,你的code看起来像:

 &LT; PHP    $页= get_page($ _ GET ['身份证']);
    呼应'&LT; H1&GT;'。 $页面级&GT;称号。'&LT; / H1&GT;';
    呼应'&LT; D​​IV&GT;'。 $页面级&gt;内容。'&LT; / DIV&GT;';?&GT;

:一个使用URL工作一样

 的index.php?ID = 5

您可以把它与搜索引擎优化的URL工作,以及在同一时间与你的旧系统保持。首先,确保的.htaccess 包含code我在上面一书中写道。

接下来,在你的文件最开始的添加以下code:

 如果(使用isset($ _ SERVER ['REDIRECT_URL'])){    $ URL =爆炸('/',$ _ SERVER ['REDIRECT_URL']);
    $ _GET ['身份证'] = $网址[0];}

什么是我们在这里干什么?前两个自己的code下去,我们基本上从旧的URL找到ID和信息,并将其输送到PHP的$ _GET变量。
我们基本上是在愚弄你的code想请求了这些变量!

剩下的唯一障碍找到所有那些讨厌的&LT; A /&GT; 标签,并取代其的href 据此,但是这是一个不同的故事。 :)

I would like to make my urls more seo friendly and for example change this:

http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce

to something nice like this:

http://www.chillisource.co.uk/product/Daves_Gourmet/Daves_Insanity_Sauce

What is the best way of going about doing this? I've had a look at doing this with the htaccess file but this seems very complicated.

Thanks in advance

解决方案

Ben Paton, there is in fact a very easy way out. CMSes like Wordpress tend to use it instead of messing around with regular expressions.


The .htaccess side

First of, you use an .htacess with the content below:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

Let me explain what it does (line by line):

  • if the apache module named mod_rewrite exists..
    • turn the module on
    • let it be known that we will rewrite anything starting after the domain name (to only rewrite some directories, use RewriteBase /subdir/)
    • if the requested path does not exist as a file...
    • and it doesn't even exist as a directory...
    • "redirect" request to the index.php file
  • close our module condition

The above is just a quick explanation. You don't really need it to use this.

What we did, is that we told Apache that all requests that would end up as 404s to pass them to the index.php file, where we can process the request manually.


The PHP side

On the PHP side, inside index.php, you simply have to parse the original URL. This URL is passed in the $_SERVER variable as $_SERVER['REDIRECT_URL'].

The best part, if there was no redirection, this variable is not set!

So, our code would end up like:

if ( isset( $_SERVER['REDIRECT_URL'] ) ) {

    $url = explode('/', $_SERVER['REDIRECT_URL'] );

    switch($url[0]){
        case 'home': // eg:    /home/
            break;
        case 'about': // eg:    /about/
            break;
        case 'images': // eg:    /images/
            switch( $url[1] ){
                case '2010': // eg:    /images/2010/
                    break;
                case '2011': // eg:    /images/2011/
                    break;
            }
            break;
    }

}


Easy Integration

I nearly forgot to mention this, but, thanks to the way it works, you can even end up not changing your existing code at all!

Less talk, more examples. Let's say your code looked like:

<?php

    $page = get_page($_GET['id']);
    echo '<h1>'. $page->title .'</h1>';
    echo '<div>'. $page->content .'</div>';

?>

Which worked with urls like:

index.php?id=5

You can make it work with SEO URLs as well as keep it with your old system at the same time. Firstly, make sure the .htaccess contains the code I wrote in the one above.

Next, add the following code at the very start of your file:

if ( isset( $_SERVER['REDIRECT_URL'] ) ) {

    $url = explode('/', $_SERVER['REDIRECT_URL'] );
    $_GET['id'] = $url[0];

}

What are we doing here? Before going on two your own code, we are basically finding IDs and information from the old URL and feeding it to PHP's $_GET variable. We are essentially fooling your code to think the request had those variables!

The only remaining hurdle to find all those pesky <a/> tags and replace their href accordingly, but that's a different story. :)

这篇关于URL重写高见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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