RewriteRule - 使用破折号获取所有网址

发布于 2024-09-04 03:56:35 字数 249 浏览 1 评论 0原文

我需要将所有带破折号的网址重定向到特定页面。
例如:
site.com/this-urlsite.com/page.php?url=this-url

RewriteRule

RewriteRule ^(.+-.+)$ page.php?url=$1

只需挂起 http。没有回应。

出了什么问题以及如何解决?

I need to redirect all urls with dash to a specific page.
For example:
site.com/this-url to site.com/page.php?url=this-url

RewriteRule

RewriteRule ^(.+-.+)$ page.php?url=$1

just hang http. No response.

What is wrong and how it can be done?

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

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

发布评论

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

评论(2

无悔心 2024-09-11 03:56:35

试试这个,你可能会遇到无限循环。

RewriteCond $0 !^page\.php
RewriteRule ^(.+-.+)?$ page.php?url=$1 [L,B,QSA]

现在:

  • 如果您已请求 page.php,则 RewriteCond 会避免匹配规则
  • QSA 标志将原始请求中的所有查询参数附加到重写的请求
  • B 标志转义反向引用 $1 以便可以安全地用作查询参数
  • L 标志并不是绝对必要的,但可以避免在匹配此规则时评估其他重写规则

Try this instead, you may have an infinite loop.

RewriteCond $0 !^page\.php
RewriteRule ^(.+-.+)?$ page.php?url=$1 [L,B,QSA]

Now:

  • The RewriteCond avoids matching the rule if you already requested page.php
  • The QSA flags appends all the query parameters from the original request to the rewritten request
  • The B flag escapes the backreference $1 so that it can safely be used as a query paramater
  • The L flag is not strictly necessary, but avoids evaluating other rewrite rules when this one is matched
若相惜即相离 2024-09-11 03:56:35

您也可以尝试此选项:

RewriteCond %{REQUEST_URI} !^page\.php
RewriteRule - page.php?url=%{REQUEST_URI} [L,B,QSA]

You may also try this option:

RewriteCond %{REQUEST_URI} !^page\.php
RewriteRule - page.php?url=%{REQUEST_URI} [L,B,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文