htaccess 在没有通用模式的 url 上重写 [英] htaccess rewrite on urls without common pattern

查看:17
本文介绍了htaccess 在没有通用模式的 url 上重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个新项目中,一个网站有一组静态 html 页面.所有者需要将它们转换为动态的.每个页面都有:一个文本部分,一张小照片以及一些指向其他页面的链接,与此相关.

In a new project, a website has a set of static html pages. The owner needs to convert them to dynamic ones. Each page has: a text section, a small photo plus some links towards other pages, related to this one.

所有这些我都可以放入数据库并创建一个动态页面(php).

All these i can put in database and create a dynamic page (php).

我的问题是每个静态页面都是独一无二的 - url 中没有共同的模式.目前有 860 个这样的页面.我可以有一个网址,如:

My problem is that each static page is unique - no common pattern in url. And there 860 such pages at the moment. I can have one url like:

folder1/folder2/item1.htm
folder1/folder2/folder3/item2.htm
folder1/folder2/folder2-fodler3/item3.htm

这 3 种是迄今为止最常见的模式(在 860 个网址中提供大约 650 个网址).

These 3 are the most common patterns so far (serving around 650 urls out of the 860).

每个文件夹名称实际上是一个类别/子类别/关键字,我可以用它来确定我的项目的属性.作为一般规则,前 2 个文件夹(文件夹 1 和文件夹 2)是产品的类别和子类别,而接下来的文件夹(文件夹 3 及之后)确定项目的部分(如果有).我希望这是有道理的.

Each folder name is actually a category/subcategory/keyword i can use to determine the properties of my item. As a general rule, the first 2 folders (folder1 and folder2) are the category and subcategory of the product, while the next folders (folder 3 and after that) determines the section (if any) of the item. I hope this makes sense.

因此,一种想法是使用旧的、静态的 url 作为数据库归档并匹配它以提供内容,例如:

So, one thought is to use the old, static, url as a database filed and match this to serve the content, something like:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /script.php?path=$1 [L]

我认为将此规则放在我的重写规则的最后将确保所有其他重写不受影响.

I think that putting this rule at the very end of my rewrite rules will ensure that all other rewrites are unaffected.

这种方法是否可取?或者还有其他方法吗?更强大/安全/轻便的东西?

Is this approach advisable? OR is there another way? Something more robust/secure/light?

推荐答案

你描述的方法很常见,也很好:

The method you describe is pretty common and well:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /script.php?path=$1 [L]

你可以通过直接在你的PHP脚本中解析原始请求字符串来减轻它的负担,这样更直接.查看您的服务器在 $_SERVER PHP 超全局数组中提供的内容,例如运行 var_dump($_SERVER);.您已经找到了与您正在使用的 $_GET['path'] 类似的东西,这通常更具互操作性:

You can lighten it up by parsing the original request string in your PHP script directly, which is more direct. Look what your server offers in the $_SERVER PHP superglobal array, e.g. run var_dump($_SERVER);. You already find something that is similar to your $_GET['path'] you're making use of, this is generally more inter-operable:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /script.php [L]

接下来,根据apache服务器版本,它可以进一步简化为:

Next to that, depending on apache server version, it can be further on simplified as:

FallbackResource /script.php

这样可以免除您当前使用的 RewriteCond 中的额外文件检查.

This spares you the extra file-check in the RewriteCond you currently use.

这篇关于htaccess 在没有通用模式的 url 上重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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