dpw30157 2014-10-04 19:23
浏览 16

如何仅为2个变量获取干净的URL?

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.

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

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

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

Also, is this how I would link to article? article.php?id=<?php echo $row_rsBlogs['id']; ?>&slug=<?php echo $row_rsBlogs['slug']; ?> or article/<?php echo $row_rsBlogs['id']; ?>/<?php echo $row_rsBlogs['slug']; ?>

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

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongpu1881 2014-10-04 19:32
    关注

    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);
    
        }
    
    }
    

    in .htaccess


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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致