htaccess 重写没有通用模式的 url
在一个新项目中,网站有一组静态 html 页面。所有者需要将它们转换为动态的。 每个页面都有:一个文本部分、一张小照片以及一些与此页面相关的其他页面的链接。
所有这些我都可以放入数据库并创建一个动态页面(php)。
我的问题是每个静态页面都是唯一的 - url 中没有通用模式。目前有 860 个这样的页面。 我可以有这样一个网址:
folder1/folder2/item1.htm
folder1/folder2/folder3/item2.htm
folder1/folder2/folder2-fodler3/item3.htm
这 3 种是迄今为止最常见的模式(为 860 个网址中的大约 650 个提供服务)。
每个文件夹名称实际上是一个类别/子类别/关键字,我可以用它来确定我的项目的属性。作为一般规则,前 2 个文件夹(文件夹 1 和文件夹 2)是产品的类别和子类别,而接下来的文件夹(文件夹 3 及之后)确定项目的部分(如果有)。 我希望这是有道理的。
因此,一种想法是使用旧的静态 url 作为数据库字段,并匹配它来提供内容,例如:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /script.php?path=$1 [L]
我认为将此规则放在重写规则的最后将确保所有其他重写不受影响。
这种方法可取吗?或者还有其他方法吗?更坚固/安全/轻便的东西?
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.
All these i can put in database and create a dynamic page (php).
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
These 3 are the most common patterns so far (serving around 650 urls out of the 860).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您描述的方法非常常见并且很好:
您可以通过直接解析 PHP 脚本中的原始请求字符串来减轻它,这更直接。查看您的服务器在
$_SERVER
PHP 超全局数组中提供的内容,例如运行var_dump($_SERVER);
。您已经找到了与您正在使用的$_GET['path']
类似的东西,这通常更具互操作性:接下来,根据 apache 服务器版本,它可以进一步简化为:
这使您无需在当前使用的
RewriteCond
中进行额外的文件检查。The method you describe is pretty common and well:
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. runvar_dump($_SERVER);
. You already find something that is similar to your$_GET['path']
you're making use of, this is generally more inter-operable:Next to that, depending on apache server version, it can be further on simplified as:
This spares you the extra file-check in the
RewriteCond
you currently use.只需使用
Just Use