2010-07-05 109 views
0

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

如:

  • /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] 

但它不起作用。

任何帮助表示赞赏。

回答

0

如果你有机会到主服务器的配置,使用此:

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

如果没有,你不得不把这个在.htaccess文件,你可以尝试

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

在无论哪种情况,这都假设你只会用一个路径组件重写URL(例如,如果你得到像/path/anything.anyextension这样的请求,它可能无法按预期工作,重写规则将需要修改以处理该请求)

+0

这将无法赶上没有扩展名的条目 – 2010-07-05 05:08:20

+0

@Ast:是的,这是所需的行为。 – 2010-07-05 06:10:29

0

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

SilverStripe(或核心,蓝宝石)提供了一个很好的方法来此,是这样的:

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