1

It's possible this question exists elsewhere, and if so, I apologize. After searching for an hour without success, I can't help but think I'm on the wrong track.

Essentially what I am looking for is a method to enforce a description or title in a page's URL. I am using CodeIgniter, so it is pretty simple to make a pretty URL go where ever I wish.

I could have:

http://mysite.com/controller/function/what-ever-i-want/can-go-here

and it will always go to:

http://mysite.com/controller/function/

with the variable values what-ever-i-want and can-go-here

What I would like is for the URL to be automatically rewritten to include the title if only the controller/function is given.

So, if someone went to:

http://mysite.com/controller/function/

it should automatically rewrite the url as

http://mysite.com/controller/function/a-more-descriptive-title/

A great example of the functionality that I am talking about is the SO URL. if you go to https://stackoverflow.com/questions/789439 it will automatically rewrite it to https://stackoverflow.com/questions/789439/how-can-i-parse-descriptive-text-to-a-datetime-object

I suspect that mod_rewrite is involved, but I would like to come up with the solution that works most gracefully with CodeIgniter.

I am very new to the pretty-url scene and desperately call upon the advice of someone with more experience. Thanks in advance for any help given!

4

2 に答える 2

1

私は CodeIgniter を使用しませんが、コントローラーの __construct にコードを配置できるはずです。CI にそれらがある場合は、ある種の事前アクション イベントにコードを配置できます。

表示されているエンティティの適切な URL を検索し、必要に応じて 301 リダイレクトを実行するだけです。

于 2012-08-30T21:15:04.783 に答える
1

Fiddler2 を使用して、Stackoverflow がこれを行う方法を確認しました。

からの応答の一部http://stackoverflow.com/questions/12205510/

HTTP/1.1 301 Moved Permanently
Location: /questions/12205510/how-can-i-enforce-a-descriptive-url-with-codeigniter
Vary: *
Content-Length: 0

したがって、基本的に に行くときは、controller/function/ユーザーを にリダイレクトする必要がありますcontroller/function/my-awesome-title。まさにそれを行う単純なコントローラーを作成しました。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Controller extends CI_Controller
{
    protected $_remap_names = array();

    function __construct()
    {
        parent::__construct();

        $this->_remap_names['func'] = "My Awesome Title";
    }

    function _remap($method, $arguments)
    {
        if(
            isset($this->_remap_names[$method])
            && sizeof($arguments) == 0
            && method_exists($this, $method)
            )
        {

            $this->load->helper("url");

            $title = str_replace(" ", "-", $this->_remap_names[$method]);
            $title = strtolower($title);

            $url   = strtolower(__CLASS__)."/$method/$title";
            $url   = site_url($url);

            // if you dont want to have index.php in url
            $url   = preg_replace("/index\.php\//", "", $url);

            header("HTTP/1.1 301 Moved Permanently");
            header("Location: $url");
            header("Vary: *");
        }
        else
        {
            call_user_func_array(array($this,$method), $arguments);
        }
    }

    function func()
    {
        echo "<h1>";
        echo $this->_remap_names[__FUNCTION__];
        echo "</h1>";
    }

};

CodeIgniters_remap関数のドキュメントは、ここの Remapping Function Calls セクションにあります。

于 2012-08-30T23:31:37.990 に答える