htaccess重写
我想将 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有权访问主服务器配置,请使用此:
如果没有,并且您被迫将其放入
.htaccess
文件中,您可以尝试在任何一种情况下,这都假设您只会重写具有单个路径组件的 URL(即,如果您收到类似
/path/anything.anyextension
的请求,它可能无法按您的预期工作,需要修改重写规则来处理该问题)If you have access to the main server configuration, use this:
If not, and you are forced to put this in a
.htaccess
file, you could tryIn 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)您需要一种更好的方法来确定何时应用重写规则,否则您的页面将无法显示外部 JS 或 CSS,除非您定义异常。
SilverStripe(或核心 Sapphire)为此提供了一个很好的方法,例如:
这要求 URI 不被空,不能是 JS、CSS 或 SWF,并重定向回您的根目录:
如果您不需要重定向,但需要处理,请删除重定向规则
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:
This requires the URI not to be empty, not to be JS, CSS or SWF, and redirects back to your root directory:
If you don't want a redirection, but the processing, remove the redirection rule
R=301