2012-03-16 131 views
2


我试图重写URL如下:的mod_rewrite不工作,因为它应该

  • www.example.com/module-name(/)=> handler.php模块=模块名称?
  • www.example.com/module-name/list/of/arguments(/)=> handler.php?module = module-name & args = list/of/arguments(我在后面的脚本中展开)

如果模块名缺失或不被允许,它将加载网站的主页。这里没有问题。
问题是mod_rewrite不能正常工作。我写这些重写规则...

RewriteEngine On 

RewriteRule ^([^/]*)/?$  handler.php?module=$1 
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 

...但强似模块名作为模块处理程序,重写引擎通过脚本本身的名称,handler.php。我在过去两天尝试了不同的正则表达式,但结果总是相同的。我不知道该做什么了!

重写规则位于.htaccess中,位于文档根目录(与handler.php一起)中,并且我在Ubuntu 11.10 64位机器上运行xampp。

在此先感谢!

回答

3

是的,发生重写之后,它进入下一个周期,而不是如你所愿,立即现有的。

您需要修改您的规则(或添加单独的条件)以忽略对handler.php文件的请求。例如:

RewriteRule ^(?!handler\.php)([^/]*)/?$ handler.php?module=$1 [L] 
RewriteRule ^(?!handler\.php)([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L] 

或有额外的单独的条件:

RewriteCond %{REQUEST_URI} !/handler\.php$ 
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L] 

RewriteCond %{REQUEST_URI} !/handler\.php$ 
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L] 

,甚至这样的:

# do not touch requests to handler.php 
RewriteRule ^handler\.php$ - [L] 

# our rewrite rules 
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L] 
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L] 

您还可以走得这样(这真的取决于你重写逻辑)

# do not do anything for requests to existing files 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .+ - [L] 

# our rewrite rules 
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L] 
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L] 
+0

哇,非常感谢您的快速和详细的答案,现在它就像一个魅力! – Francesco 2012-03-16 15:34:59

+0

@Ceppo很高兴为你效劳。现在请选择其中一个答案并将其标记为已接受,thnx – LazyOne 2012-03-16 18:49:52

+0

完成。对不起,我以前没有用过StackOverflow :) – Francesco 2012-03-17 11:12:14

1

一个问题可能是现有的文件名也会被重写。为了避免这种情况,你可以添加:所以你

RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files 
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories 

这将是:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files 
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories 
RewriteRule ^([^/]*)/?$  handler.php?module=$1 

RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files 
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories 
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 
+0

也谢谢你的支持swer! – Francesco 2012-03-16 15:35:18

相关问题