【问题标题】:Renaming my PHP website URL using htaccess使用 htaccess 重命名我的 PHP 网站 URL
【发布时间】:2019-06-22 00:27:52
【问题描述】:

我想使用 .htaccess 文件重命名我网站上的 URL: 从http://siteaddress.com/city.php?city=londonhttp://siteaddress.com/city/city-london

例如,重命名其他城市的方法也相同; ../city.php?city=birmingham...

【问题讨论】:

标签: apache url mod-rewrite


【解决方案1】:

我使用 MVC 架构模式来做到这一点。这里有一些入门资源,可以帮助您到达需要的地方。可能有更好的方法,但这是一种方法。

Starter Code: panique/mini

main routing Algorithm

这里给出算法的要点是 Public 文件夹中的 .htaccess 文件。

# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

在 Application.php 中使用以下算法处理路由

<?php
class Application
{
    /** @var null The controller */
    private $url_controller = null;
    /** @var null The method (of the above controller), often also named "action" */
    private $url_action = null;
    /** @var array URL parameters */
    private $url_params = array();

    /**
     * "Start" the application:
     * Analyze the URL elements and calls the according controller/method or the fallback
     */
    public function __construct()
    {
        // create array with URL parts in $url
        $this->splitUrl();
        // check for controller: no controller given ? then load start-page
        if (!$this->url_controller) {
            require APP . 'controller/home.php';
            $page = new Home();
            $page->index();
        } 
        elseif (file_exists(APP . 'controller/' . $this->url_controller . '.php')) {
            // here we did check for controller: does such a controller exist ?
            // if so, then load this file and create this controller
            // example: if controller would be "city", then this line would translate into: $this->city= new City();
            require APP . 'controller/' . $this->url_controller . '.php';
            $this->url_controller = new $this->url_controller();
            // check for method: does such a method exist in the controller ?
            if (method_exists($this->url_controller, $this->url_action)) {
                if (!empty($this->url_params)) {
                    // Call the method and pass arguments to it
                    call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params);
                } else {
                    // If no parameters are given, just call the method without parameters, like $this->city->method();
                    $this->url_controller->{$this->url_action}();
                }
            } else {
                if (strlen($this->url_action) == 0) {
                    // no action defined: call the default index() method of a selected controller
                    $this->url_controller->index();
                }
                else {
                    header('location: ' . URL . 'problem');
                }
            }
        } else {
            header('location: ' . URL . 'problem');
        }
    }

    private function splitUrl()
    {
        if (isset($_GET['url'])) {
            // split URL
            $url = trim($_GET['url'], '/');
            $url = filter_var($url, FILTER_SANITIZE_URL);
            $url = explode('/', $url);

            // Put URL parts into according properties
            $this->url_controller = isset($url[0]) ? $url[0] : null;
            $this->url_action = isset($url[1]) ? $url[1] : null;

           // Remove controller and action from the split URL
            unset($url[0], $url[1]);

            // Rebase array keys and store the URL params
            $this->url_params = array_values($url);
        }
    }

如果您的网址如下所示,那么它们将被分解为

http://siteaddress.com/cities/city/london

控制器:城市

方法:城市

参数:伦敦

http://siteaddress.com/cities/city/birmingham

控制器:城市

方法:城市

参数:伯明翰

这是项目结构的快照

为了更好地理解,我使用了这个播放列表。 YOUTUBE: Build a PHP MVC Application: Introduction (Part 1/9)

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多