简体   繁体   English

使用PHP处理站点范围的URL重写

[英]Handling site-wide URL rewrites with PHP

I'm looking into the feasibility of using PHP - instead of mod_rewrite - to handle URL canonicalization. 我正在研究使用PHP(而不是mod_rewrite )来处理URL规范化的可行性。 I'm looking to be able to map a large number of different URLs to a given physical PHP page, and handle 301's and 404's in a more centralized and maintainable way. 我希望能够将大量不同的URL映射到给定的物理PHP页面,并以更集中和可维护的方式处理301和404。 This will include common misspellings, aliases, search engine friendly URL parameters, and the like. 这将包括常见的拼写错误,别名,搜索引擎友好的URL参数等。 These needs seem well outside the power of mod_rewrite , so I'm looking into other options. 这些需求似乎超出了mod_rewrite ,因此我正在研究其他选项。

I'm thinking I would create a canonical.php script which I map every page to with the following in .htaccess ( borrowed from this post ): 我想我将创建一个canonical.php脚本,将每个页面映射到.htaccess中的以下内容( 从本文中借用 ):

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ canonical.php/$1?%{QUERY_STRING} [L]

And then canonical.php would do whatever URL parsing / db lookups / redirects / etc. are necessary, then simply include /the/appropriate/file.php for the given request. 然后canonical.php会执行所有必要的URL解析/ db查找/重定向/等操作,然后为给定请求简单地包含/the/appropriate/file.php

Is this a reasonable course of action? 这是合理的做法吗? Is such functionality actually feasible with mod_rewrite directly? 直接使用mod_rewrite这样的功能实际上可行吗? (DB lookups and the like aside) will this be distinctly slower than mod_rewrite ? (除了数据库查找等),这会明显比mod_rewrite慢吗? Is there any other methodology that's more robust than a PHP wrapper? 还有其他方法比PHP包装器更强大吗?

You're talking about routing, which plenty of frameworks do. 您正在谈论路由,许多框架都在这样做。 Take a look at this answer: https://stackoverflow.com/questions/115629/simplest-php-routing-framework 看看这个答案: https : //stackoverflow.com/questions/115629/simplest-php-routing-framework

What I would suggest and what I use is a php file that gets called everytime when a 404 is encountered and it stores the url encountered. 我建议和使用的是一个每次遇到404时都会调用的php文件,它存储遇到的url。 Then once a week I go to the management console and I map the wrongly spelled mistyped, old urls, searchengine's history url's to the current existing urls then I hit the parse button and it spews out a new updated .htaccess file out for me. 然后,每周一次,我进入管理控制台,然后将拼写错误的错误键入的旧url,搜索引擎的历史url映射到当前现有的url,然后单击“解析”按钮,它为我弹出一个新的更新的.htaccess文件。

That way you lighten the load on your database, loading time, compiling time and redirecting time. 这样,您可以减轻数据库的负载,加载时间,编译时间和重定向时间。

Just my 2 cents. 只是我的2美分。

还没有机会进行测试,但是这个答案使我想到了Apache的FallbackResource指令,这听起来很有希望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM