htaccess重写

发布于 2024-09-08 19:49:02 字数 476 浏览 1 评论 0原文

我想将 /anything.anyextension 重写为 /?post=anything

例如:

  • /this-is-a-post.php/?post=this-is-a-post
  • /this-is-a-post .html/?post=this-is-a-post 甚至
  • /this-is-a-post//?post =this-is-a-post

我尝试过

RewriteRule ^([a-zA-Z0-9_-]+)(|/|\.[a-z]{3,4})$ ?$1 [L]

,但不起作用。

任何帮助表示赞赏。

I would like to rewrite /anything.anyextension into /?post=anything.

eg:

  • /this-is-a-post.php into /?post=this-is-a-post or
  • /this-is-a-post.html into /?post=this-is-a-post or even
  • /this-is-a-post/ into /?post=this-is-a-post

I tried

RewriteRule ^([a-zA-Z0-9_-]+)(|/|\.[a-z]{3,4})$ ?$1 [L]

but it doesn't work.

Any help appreciated.

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

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

发布评论

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

评论(2

风透绣罗衣 2024-09-15 19:49:02

如果您有权访问主服务器配置,请使用此:

RewriteRule ^/(.+)\.\w+$ /?post=\1 [L]

如果没有,并且您被迫将其放入 .htaccess 文件中,您可以尝试

RewriteRule ^(.+)\.\w+$ /?post=\1 [L]

在任何一种情况下,这都假设您只会重写具有单个路径组件的 URL(即,如果您收到类似 /path/anything.anyextension 的请求,它可能无法按您的预期工作,需要修改重写规则来处理该问题)

If you have access to the main server configuration, use this:

RewriteRule ^/(.+)\.\w+$ /?post=\1 [L]

If not, and you are forced to put this in a .htaccess file, you could try

RewriteRule ^(.+)\.\w+$ /?post=\1 [L]

In either case, this assumes you will only be rewriting URLs with a single path component (i.e. if you get a request like /path/anything.anyextension it might not work as you expect, the rewrite rule would need to be modified to handle that)

赏烟花じ飞满天 2024-09-15 19:49:02

您需要一种更好的方法来确定何时应用重写规则,否则您的页面将无法显示外部 JS 或 CSS,除非您定义异常。

SilverStripe(或核心 Sapphire)为此提供了一个很好的方法,例如:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css)|(\.js)|(\.swf)$ [NC]
RewriteCond %{REQUEST_URI} .+
RewriteRule ^([^\.]+) /?post=$1 [L,R=301]

这要求 URI 不被空,不能是 JS、CSS 或 SWF,并重定向回您的根目录:

http://localhost/this-is-a-post.php
http://localhost/?post=this-is-a-post

如果您不需要重定向,但需要处理,请删除重定向规则 R=301

You need a better way to determine when to apply the rewrite rule, otherwise your page won't be able to display external JS or CSS, unless you define an exception.

SilverStripe (or the core, Sapphire) offers a good approach to this, something like:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css)|(\.js)|(\.swf)$ [NC]
RewriteCond %{REQUEST_URI} .+
RewriteRule ^([^\.]+) /?post=$1 [L,R=301]

This requires the URI not to be empty, not to be JS, CSS or SWF, and redirects back to your root directory:

http://localhost/this-is-a-post.php
http://localhost/?post=this-is-a-post

If you don't want a redirection, but the processing, remove the redirection rule R=301

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