滥用 Mod Rewrite 以获得干净的文件系统和漂亮的 URL

发布于 2024-09-11 20:29:44 字数 990 浏览 2 评论 0原文

我有一些 php 文件,它们执行一些不同的工作。我想改变我的客户访问这些 php 文件的方式,使其对最终用户来说更加干净。 Mod_Rewrite 系统已经表明,如果掌握在正确的服务器管理员手中,它可以做一些非常强大的事情。所以我想知道你可以在多大程度上滥用 Mod Rewrite 规则来获得更干净的文件系统和漂亮的 URL。考虑到 PHP 文件本身使用查询字符串来获取数据,我想根据我们对假文件系统的深入程度来为查询字符串的构建方式设置别名。

我们网站的 URL 是 http://www.domain.tld/,但我们简称为 domain.tld。我想将几个不同的地址映射到几个不同文件上的几个不同的查询字符串。但我也想随心所欲地进行扩展。

或者第一组是,任何经过 domain.tld/series/ 的内容都应该定向到 domain.tld/series.php 脚本,其中包含过去系列的任何(假)目录成为 series.php 查询字符串的一部分。对于指向 domain.tld/users/ 方向且应重定向到 domain.tld/users.php 文件的任何内容,也应该发生同样的情况。

因此,如果我们有一个类似 domain.tld/series/Master/2010/domain.tld/series/Novice/Season 01/ 的 URL,它们仍然会被重定向到domain.tld/series.php 脚本,但查询字符串为 ?0=Master&1=2010?0=Novice&1=第 01 季。但如果我想了解 Master 系列的概述,我可以访问 URL domain.tld/series/Master/ 并生成 ?0=Master< 的查询字符串/代码>。这个想法是重写规则应该允许无限的可扩展性。

I have a few php files that do a few different jobs. I'd like to change the way my clients access these php files to make it more clean for the end user. The Mod_Rewrite system has shown that it can do some pretty powerful things when in the hands of the right server admin. So I was wondering how far can you abuse the Mod Rewrite rules for a cleaner file system, and pretty URLs. Considering that the PHP files themselves use query strings to get their data, I'd like to alias the way the query string is built based upon how the how deep into the fake files system we go.

Our website's URL is http://www.domain.tld/, but we shall call it domain.tld for short. I'd like to map a few different address to a few different query strings on a few different files. But I'd also like to to be expandable on a whim.

Or first set would be, anything going past domain.tld/series/ should be directed to the domain.tld/series.php script with any (fake) directory past series to become part of the query-string for series.php. The same should happen to anything directed in the direction of domain.tld/users/ that should be redirected to the domain.tld/users.php file.

So if we had a URLs like, domain.tld/series/Master/2010/ or domain.tld/series/Novice/Season 01/ they would still be redirected to the domain.tld/series.php script, but with the query-string of ?0=Master&1=2010 and ?0=Novice&1=Season 01. But should I want to get an overview of the Master series, I could go the the URL domain.tld/series/Master/ and produce the query-string of just ?0=Master. The idea being that the rewrite rule should allow for infinite expandability.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-09-18 20:29:45

我就是这样做的,而且它确实可以无限工作:

RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]

诀窍是整个路径作为单个参数 q 传递到index.php。例如,domain.tld/series/Novice/Season 01/ 变为 domain.tld/?q=series/Novice/Season 01。然后你可以做:

<?php
$params = explode('/', $_GET['q']);
var_dump($params);
?>

获取各个零件。

array(3) { 0 => 'series', 1 => 'Novice', 2 => 'Season 01' }

This is how I'm doing it, and it sure works infinitely:

RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]

The trick is that the whole path is passed on as a single parameter, q, to index.php. So for example domain.tld/series/Novice/Season 01/ becomes domain.tld/?q=series/Novice/Season 01. Then you can do:

<?php
$params = explode('/', $_GET['q']);
var_dump($params);
?>

to get the individual parts.

array(3) { 0 => 'series', 1 => 'Novice', 2 => 'Season 01' }
我三岁 2024-09-18 20:29:45

在这样的系统中不可能完全动态,并且具有如您所说的“无限可扩展性”。您必须为 URL 中允许的每个“层”定义一个 RewriteRule,或者将第一个“层”之后的所有内容作为单个变量进行匹配,并使用 PHP 进行工作。

示例 1

RewriteRule ^([^/]+)/?$ /$1.php
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?0=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?0=$2&1=$3

示例 2

RewriteRule ^([^/]+)/(.*)/? /$1.php?qs=$2

显然,这些只是非常简单的示例,您可能必须使用 RewriteConds 等来免除某些文件等。

It is not possible to be completely dynamic in such a system and have, as you say 'infinite expandability. You would have to define a RewriteRule for every 'tier' you will allow in your URL, or alternatively match everything after the first 'tier' as a single variable and do the work with PHP.

Example 1

RewriteRule ^([^/]+)/?$ /$1.php
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?0=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?0=$2&1=$3

Example 2

RewriteRule ^([^/]+)/(.*)/? /$1.php?qs=$2

Obviously these are only very simple examples and you'd probably have to use RewriteConds etc. to exempt certain files etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文