我如何获得干净的网址只有2个变量? [英] How do I get clean URLS for only 2 variables?

查看:166
本文介绍了我如何获得干净的网址只有2个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到/阅读与清洁的.htaccess网址的问题,但对我的生活,我不能让他们的工作为我的特殊需求。我不断收到404消息。

I've seen/read questions about clean URLs with .htaccess, but for the life of me, I cannot get them to work for my specific needs. I keep getting 404 message.

例:www.mysite.com/article.php?id=1&title=my-blog-title

Example: www.mysite.com/article.php?id=1&title=my-blog-title

我想为网址为:www.mysite.com/article/1/my-blog-title

I would like for url to be: www.mysite.com/article/1/my-blog-title

下面是我到目前为止在我的.htaccess:

Here's what I have so far in my .htaccess:

 Options -MultiViews
#DirectorySlash on
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L]


# Rewrite for article.php?id=1&title=Title-Goes-Here 
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [NC,L]

#Rewrite for certain files with .php extension
RewriteRule ^contact$ contact.php
RewriteRule ^blogs$ blogs.php
RewriteRule ^privacy-policy$ privacy-policy.php
RewriteRule ^terms-of-service$ terms-of-service.php

另外,请问这是怎么我会链接到文章? article.php ID =<?PHP的echo $ row_rsBlogs ['身份证']; ?>&安培;塞=< PHP的echo $ row_rsBlogs ['塞']; ?> 文章/<?PHP的echo $ row_rsBlogs ['身份证']; ?> /<?PHP的echo $ row_rsBlogs ['塞']; ?>

我使用Dreamweaver,但我很舒服手工编码。

I'm using Dreamweaver, but I am comfortable hand coding.

在此先感谢。

推荐答案

您可以使用告诉web服务器调度程序的所有请求重定向到如的index.php .. 在有调度实例analizes请求,并调用某些控制器(如articlesControllers)

You could use a dispatcher by telling the webserver to redirect all request to e.g. index.php.. In there a dispatch instance analizes the request and invokes certain controllers (e.g. articlesControllers)

class Dispatcher

{

    // dispatch request to the appropriate controllers/method

    public static function dispatch()

    {

        $url = explode('/', trim($_SERVER['REQUEST_URI'], '/'), 4);

        /*
         * If we are using apache module 'mod_rewrite' - shifting that 'request_uri'-array would be a bad idea :3
         */
        //array_shift($url);

        // get controllers name

        $controller = !empty($url[0]) ? $url[0] . 'Controller' : 'indexController';

        // get method name of controllers

        $method = !empty($url[1]) ? $url[1] : 'index';

        // get argument passed in to the method

        $parameters = array();

        if (!empty($url[2])) {

            $arguments = explode('/', $url[2]);

            foreach ($arguments as $argument) {
                $keyValue = explode('=',$argument);
                $parameters[$keyValue[0]] = $keyValue[1];
            }

        }


        // create controllers instance and call the specified method

        $cont = new $controller;
        if(!method_exists($cont,$method)) {
            throw new MethodNotFoundException("requested method \"". $method . "\" not found in controller \"" . $controller . "\"");
        }
        $cont->$method($parameters);

    }

}

在.htaccess

in .htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php

这篇关于我如何获得干净的网址只有2个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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