帮助在动态和静态 URL 上使用 mod_rewrite

发布于 2024-09-29 17:27:30 字数 806 浏览 0 评论 0原文

我查看了这个主题:配置搜索引擎友好的网址(mod_rewrite)并了解如何应用重写规则,但需要帮助了解如何将它们应用于具有静态和动态 URL 的网站。

从上面的主题来看,重写规则将首先查找 URL 中存在的文件,并将其传递。这样就可以覆盖我的静态页面。

问题 1:索引会被保留吗(换句话说,domain.com/foo 将显示domain.com/foo/index.php)?

问题2:有没有办法将单个模板文件与这些静态页面一起使用(因此静态文件实际上只是内容)?目前,我的重写规则将所有 URL 都指向一个主模板文件,该文件查找所请求的文件,然后包含它。

继续,重写规则会将您重定向到 index.php,以查找与上述规则不匹配的任何 URL。我认为这就是我希望我的代码确定它是否是我的网站的实际 URL 的地方,并相应地传递内容。

问题 3:重定向到 index.php 不会传递任何要处理的变量。如果我使用index.php?url=$1,这会同时考虑domain.com/hi 和domain.com/hi/there 吗?

问题 4:我可以采取哪些步骤来确保处理的 URL 是合法的(而不是 xss 攻击或类似的攻击)?我是否应该在 MySQL 数据库中设置一个单独的表来存储所有可能的 URL 以及内容的 ID?我想这就是 WordPress 的工作原理。或者,如果不使用 MySQL,我会使用类或数组来执行此操作吗?

我知道很多,提前感谢大家的帮助!

I looked at this topic: configuring search engine friendly url (mod_rewrite) and understand how the rewrite rules would apply, but need help understanding how to adopt them for websites that have static and dynamic URL's.

From the topic above, the rewrite rules would first look for a file that existed in the URL, and deliver it. So that would cover my static pages.

Question 1: would indexing be preserved (in other words, domain.com/foo would show domain.com/foo/index.php)?

Question 2: is there a way to use a single template file with these static pages (so the static file is really only the content)? At the moment, my rewrite rules point all URL's to a main template file that looks for the requested file and then includes it.

Moving on, the rewrite rules would redirect you to index.php for any URL's that didn't match the above rule. I assume that's where I'd want my code to determine if it's an actual URL for my site, and deliver the content accordingly.

Question 3: redirecting to index.php isn't going to pass along any variables to process. If I used index.php?url=$1 would that account for both domain.com/hi and domain.com/hi/there?

Question 4: what steps can I take to ensure the URL's processed are legitimate (and not xss attacks or anything like that)? Should I set up a separate table in my MySQL database that stores all the possible URL's along with the ID of the content? I assume that's how Wordpress works. Or, if not using MySQL, would I do this with a class, or array?

I know it's a lot, thanks in advance for everyone's help!

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

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

发布评论

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

评论(1

三生殊途 2024-10-06 17:27:32

仅供记录:如果没有另外提及,以下所有答案均参考 jiewmeng的配置搜索引擎友好的url(mod_rewrite)

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

问题 1:是否会保留索引(换句话说,domain.com/foo 将显示domain.com/foo/index.php)?

如果引用 DirectoryIndex 指定的索引文档,那么,是的,因为 REQUEST_FILENAME 将引用现有目录,比较器 -f 将产生 true 并且不会发生重写。

问题2:有没有办法将单个模板文件与这些静态页面一起使用(因此静态文件实际上只是内容)?目前,我的重写规则将所有 URL 都指向一个主模板文件,该文件查找所请求的文件,然后包含它。

不,那是不可能的。未经 handler 进一步处理的静态文件直接发送到客户端。

问题 3:重定向到 index.php 不会传递任何要处理的变量。如果我使用index.php?url=$1 会同时考虑domain.com/hi 和domain.com/hi/there 吗?

在上述示例中,您可以使用 $_SERVER['REQUEST_URI'] 获取原始请求的 URI 路径和查询字符串,如 请求行。因此,即使是原始请求的查询字符串也会被保留。

问题 4:我可以采取哪些步骤来确保处理的 URL 是合法的(而不是 xss 攻击或类似的攻击)?我是否应该在 MySQL 数据库中设置一个单独的表来存储所有可能的 URL 以及内容的 ID?我想这就是 WordPress 的工作原理。或者,如果不使用 MySQL,我会使用类或数组来执行此操作吗?

是的,您绝对应该知道哪些 URL 有效,哪些 URL 无效。一般来说,应用程序通过解析请求并尝试查找所请求的资源来做出此决定。如何完成此操作取决于您的应用程序的功能及其执行方式。

Just for the record: If not mentioned otherwise, all following answers refer to these rules that were mentioned in jiewmeng’s configuring search engine friendly url (mod_rewrite):

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Question 1: would indexing be preserved (in other words, domain.com/foo would show domain.com/foo/index.php)?

If you refer to index documents specified by DirectoryIndex, then, yes, as REQUEST_FILENAME would refer to an existing directory, the comparator -f would yield true and no rewrite would take place.

Question 2: is there a way to use a single template file with these static pages (so the static file is really only the content)? At the moment, my rewrite rules point all URL's to a main template file that looks for the requested file and then includes it.

No, that is not possible. Static files that are not further processed by a handler are directly sent to the client.

Question 3: redirecting to index.php isn't going to pass along any variables to process. If I used index.php?url=$1 would that account for both domain.com/hi and domain.com/hi/there?

In the mentioned example you can use $_SERVER['REQUEST_URI'] to get the original requested URI path and query string as it appeared in the request line. So even the original requested query string will be preserved.

Question 4: what steps can I take to ensure the URL's processed are legitimate (and not xss attacks or anything like that)? Should I set up a separate table in my MySQL database that stores all the possible URL's along with the ID of the content? I assume that's how Wordpress works. Or, if not using MySQL, would I do this with a class, or array?

Yes, you should definitely know what URLs are valid and what are invalid. And in general it’s the application that does this decision by parsing the request and trying to find the requested resource. How that is done depends on what your application does and how it does that.

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