编写.htaccess规则

发布于 2024-10-02 11:22:29 字数 758 浏览 0 评论 0原文

我已经有了 url 中包含参数的网站。现在我正在考虑为用户提供干净的网址以避免网址中的参数。我研究了 .htaccess 规则(mod 重写),但我不明白的一件事是,我是否需要更改所有位置的 url,以便 htaccess 中的规则起作用?我觉得这更乏味,因为我需要找到这些地方并将其更改为清除网址。例如,我有 http://www.mysite.com/index.php?view=关于http://www.mysite.com/index.php?view =联系。我编写了该规则,因为

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?view=$1 [L]

如果我输入 mystie.com/about.html 或 mysite.com/contact.html,则效果很好。但现在的问题是我需要找到编码中的位置并将所有 view=about 或 view=contact 更改为 about.html 或 contact.html。在这种情况下,它可能很简单,因为它只是我需要更改的标头..但是如果有像 view=showdata?user=dsdsa&aga=12 这样的 url...我找不到提到这一点的所有地方并改变。有什么简单的方法或聪明的方法吗?感谢您的帮助。

I already have the site with parameters in the url. now i am thinking to do the clean url for the users to avoid the parameters in the url. I studied about the .htaccess rules(mod rewrite) but one thing i don't understand is, do i need to change the url's in all the places so that rules in htaccess will work? I feel this is more tedious as I need to find the places and change it to clear url. For example, I have http://www.mysite.com/index.php?view=about and http://www.mysite.com/index.php?view=contact. I wrote the rule as

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?view=$1 [L]

this works fine if i go and type mystie.com/about.html OR mysite.com/contact.html. But the thing is now I need to find the place in the coding and change all view=about or view=contact to about.html or contact.html. In this case it might be simple as it will be just the header i need to change..but if there are url's like view=showdata?user=dsdsa&aga=12...i cannot find all the places where this is mentioned and change. any easy way or smart approach? thanks for your help.

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

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

发布评论

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

评论(3

风流物 2024-10-09 11:22:29

您将不得不重新设计您的代码。

但是,有机会以编程方式以性能为代价进行重新映射。

由于您在 PHP 上运行,因此您可以使用输出缓冲的 ob 函数来:

  1. 在启动时缓冲输出
  2. 检索缓冲区内容,并在呈现所有页面后清除它
  3. 应用正则表达式来转换 URL
  4. 将修改后的缓冲区写入输出流

显然,您需要在 PHP 脚本中映射一组规则。很抱歉没有为您提供任何代码,但我几年前根据我找到的其他代码这样做了。但方法很简单。

You would have to re-engineer your code.

However, there are chances to do the remap programmatically at cost of performance.

Since you run on PHP, you can use the ob functions of output buffering to:

  1. Buffer the output on startup
  2. Retrieve the buffer contents, and clear it, once you rendered all the page
  3. Apply a regex to convert URLs
  4. Write the modified buffer to output stream

You obviously need a set of rules to be mapped in a PHP script. Sorry for providing you with no code but I did this years ago, basing on other code I found. But the approach is straightforward.

一影成城 2024-10-09 11:22:29

重写规则只会影响以 .html 结尾的 URL。因此,您直接指向 index.php 的链接应该保持完全有效。然而,这些丑陋的链接会被搜索引擎发现并出现在结果页面中。

根据您当前的重写规则,包含 user=dsdsa 等参数的 URL 无法重写为干净 URL(例如 重写为 /showdata.html?user=dsdsa)。这是因为,如果您在重写规则的替换子字符串中包含查询字符串,Apache 将删除其他参数。您可以使用 [QSA] 标志来避免这种情况。有关更多详细信息,请参阅 Apache 文档

我无法说出如何找到 PHP 代码中所有丑陋的链接。您可能必须查看 view= 存在的每个位置并手动更新它。

The rewrite rule will only affect URLs that end in .html. Therefore, your links directly to index.php should remain fully working. Those ugly links, though, would be picked up by search engines and appear in results pages.

Under your current rewrite rule, URLs that include parameters like user=dsdsa cannot be rewritten as clean URLs (e.g. as /showdata.html?user=dsdsa). This is because if you include a query string in the rewrite rule's replacement substring, Apache will delete the other parameters. You can avoid this by using the [QSA] flag. See Apache documentation for more details.

I cannot say how to find all the ugly links in the PHP code. You might have to resort to looking at each place where view= exists and manually updating it.

眼眸印温柔 2024-10-09 11:22:29

如果我理解您的问题,那么您是在询问是否需要更改代码中的所有硬编码 URL。

这种情况下的答案是否定的,您不需要更改 URL,更改 URL 时使用 mod_rewrite 的优点是旧 URL 保持活动状态。它并不介意您直接在浏览器中输入 URL,或者单击页面中的链接,它们会生成相同的 HTTP 请求(引荐来源网址除外)。

当然,您可以逐渐更改代码,使其使用真实的 URL,以避免混淆。

If I understand your question, you are asking if you need to change all the hardcoded URL's that you have in your code.

The answer in this case is no, you don't need to change the URL's, the advantage of using mod_rewrite when changing URL's is that the old URL's remain active. It doesn't mind if you type the URL directly in the browser, of you click a link in a page, they generate the same HTTP request (with the exception of the referrer).

Of course, you could change your code gradually so it uses the real URL's, in order to avoid confusion.

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