网址上没有共同的模式htaccess的重写重写、模式、网址、htaccess

2023-09-02 09:40:48 作者:绘川绮

在一个新的项目,一个网站有一组静态的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是最常见的模式迄今(服务大约650网址出860)。

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

每个文件夹的名称实际上是一个类/子/关键字我可以用它来确定我的项目的属性。作为一般规则,前2个文件夹(文件夹1和FOLDER2)是该产品的类别和子类别,而下一个的文件夹(文件夹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.

于是,人们想到的是用旧的,静态的,网址存档数据库并匹配该服务的内容,是这样的:

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?

推荐答案

您所描述的方法是pretty的共同和幸福:

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 ['路径'] 你利用,这是一般更可互操作的:

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.