donglan9517 2012-12-03 18:27
浏览 38
已采纳

如何使用数据库值更改旧php站点上的URL结构?

I've been tasked with making the URLs for an old website "pretty"

It's a real estate website with a few dozen buildings and maybe 100ish total apartment listings.

Current urls are like: (i was able to remove .php extension easily with .htaccess)

example.com/about?building-id=65
example.com/about?apartment-id=35

It gets considerably more complicated than that, with some search pages, different listings views, a contact form page that gets sent a building or apt id to prefil the form with some info, etc...

But for this example...

Ideally the URLs should be

example.com/about/building-name/
example.com/about/building-name/apartment-name

The building name and apartment name are both values stored in the database, with the id#s as the primary keys.

After researching this issue a bit, I've determined a couple different approaches

1) Dynamically Generate the .htaccess file upon changes in admin

  • I think I can do this right?
  • It would explicitly rewrite just about every possible valid query string possibility ( up to couple hundred probably)
  • I imagine this will cause some performance issues, and is probably not best practice.
  • If its the easiest option, I'm ok with it being sloppy and bad practice as long as it works.

2) Create a controller and rewite all queries to index.php which would put together all the views from here.

  • This is a bit more out of my comfort zone, but probably considered the best practice?

The site is seriously old, and poorly put together (1000s of lines of custom (non OOP) php with lots of code duplication) For that matter, its really 2 different sites sharing the one database, and most of the code base (2nd was created some time ago with a copy-paste of original code base and has since grown apart)


My Question(s)

  • Are these viable options and did I miss any alternative?

  • Which approach should I take?

  • 写回答

1条回答 默认 最新

  • douqian6194 2012-12-03 20:02
    关注

    The actual generation of the .htaccess file wouldn't be an issue. I mean, writing 500 or 1000 lines of text to a file isn't such a big deal at all. However, the multitude of rules may actually very well give a performance hit, as the engine would check the requested URL against each for every HTTP request. It's probably not a very big performance hit, but it seems wasteful to me. Note that Apache, AFAIK, does no optimization on .htaccess rewrite rules or redirects, i.e. it doesn't build a big regex out of the small regexes, etc.

    Option 2, parsing the request in PHP, is actually less work, I think, especially with a huge non-OOP site.

    Consider a snippet like this:

    $uri = explode('/', $_SERVER['REQUEST_URI']);
    if($uri[0] == 'about') {
        $id_aray = lookup_building_and_apartment_by_name( $uri[1], $uri[2] );
        $_GET['building_id'] = $id_array['building'];
        $_GET['apartment_id'] = $id_array['apartment'];
        include('about.php');
    } 
    else if($uri[0] == 'something_else') {
        // something else.
    }
    else {
        // 404
    }
    

    Such a logic would serve perfectly well as a controller, and it isn't such a big work.

    Alternarively, you could put a URI-parser snippet at the top of every entry point. Like, at the top of about.php:

    $uri = explode('/', $_SERVER['REQUEST_URI']);
    $id_aray = lookup_building_and_apartment_by_name( $uri[1], $uri[2] );
    $_GET['building_id'] = $id_array['building'];
    $_GET['apartment_id'] = $id_array['apartment'];
    

    This would enable you to work incrementally, one subpage at a time, and the old links would continue to work as well.


    Mostly for the record: there's an option 3, you can use capture groups in mod_rewrite rules, like so:

    RewriteRule ^/about/([^/]+)/([^/]+)$ about.php?building_name=$1&apt_name=$2 [NC,L]
    RewriteRule ^/about/([^/]+)$ about.php?building_name=$1 [NC,L]
    

    (This can be done with a single rule, but it's considerably easier to read & write like this.) This still requires you to have a php-based translation in about.php, but has the advantage that you can put these rules in your httpd.conf.

    There's also an option 4: RewriteMap. It's an awesome and powerful tool, and it may be just the thing for you, if you're able to cope with the terseness of the Apache manual. Learning to use it would probably take a lot more time than using the PHP-based solution, but it's a good thing to know, it's an efficient solution, and it's very elegant.

    You could easily generate a text-based mapping, then use the mapping in the RewriteRule, like so:

    RewriteMap building_map txt:/etc/apache2/buildings.txt
    RewriteRule ^/about/([^/]+)$ about.php?building-id=${building_map:$1|0} [NC,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题