Mod 重写 url

发布于 2024-09-03 11:25:33 字数 180 浏览 2 评论 0原文

我在这里束手无策,我通常喜欢自己解决问题,但这让我在这里彻底失败了。 我正在尝试将包含加号的网址重写...

/search.php?q=can+be+any+length

/can-be-any-length.html

任何帮助将非常感激,因为重写了 + php 不是一个选项

I'm at my wits end here , I normally like to work things out on my own but this has me well and truly beaten here..
I'm trying to mod rewrite my urls that contain pluses...

/search.php?q=can+be+any+length

to

/can-be-any-length.html

Any help would be really appreciated becaus rewriting the + php is not an option

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

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

发布评论

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

评论(2

鹿港巷口少年归 2024-09-10 11:25:33

使用 mod_rewrite 进行此类工作并不是最佳选择,因为您一次只能替换固定数量的字符。使用 PHP 肯定会更容易。

但是您可以使用 "N"(下一个) 标志重新启动替换,导致重写引擎在查询字符串中有“+”时循环:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*\+.*)
RewriteRule ^ %{REQUEST_URI}?%1q=%3-%4 [N]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*)&*(.*)$
RewriteRule ^ /%3-%4.html?%1%5 [L,R=301]

Using mod_rewrite for this kind of work is not the best option as you can only replace a fixed amount of characters at at time. Using PHP would certainly be easier.

But you can use the "N" (Next) flag to restart replacement, causing the rewrite engine to loop while there's a "+" in the query string:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*\+.*)
RewriteRule ^ %{REQUEST_URI}?%1q=%3-%4 [N]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*)&*(.*)$
RewriteRule ^ /%3-%4.html?%1%5 [L,R=301]
流云如水 2024-09-10 11:25:33

也许使用 RewriteMap :

RewriteMap mymap txt:/path/to/file.txt
RewriteRule ^(.*).html$ search.php?q=${mymap:$1}

文件 /path/to/file.txt 的内容

- +

Maybe with RewriteMap :

RewriteMap mymap txt:/path/to/file.txt
RewriteRule ^(.*).html$ search.php?q=${mymap:$1}

Content of file /path/to/file.txt

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