mod_rewrite和mod_dir碰撞

时间:2011-07-04 02:22:10

标签: mod-rewrite apache2

我正在将我的htaccess编辑为内部重定向 几乎任何到php页面处理程序的URL:

RewriteRule ^images\/ - [L,NS]
RewriteRule ^docs\/ - [L,NS]
RewriteRule ^([A-Za-z0-9\_\-]+)\/?$ pages/pagehandler.php?page=$1 [L,QSA,NS]

这样可以正常接受在地址中输入的目录 由于某种原因没有尾部斜杠的条获取重复的查询字符串, 由于某种原因,浏览器的地址栏发生了变化 例如,如果我输入网址:

本地主机/电影

如果站点根目录中有一个名为movies的目录 而地址变为:

本地主机/电影/?页=电影

我猜这是与mod_dir的一些碰撞,但我不知道 如果我使用,如何克服它:

<IfModule mod_dir.c>
   DirectorySlash Off
</IfModule>

比它有效,但我不想要这个,我认为由于某种原因这个网址 被重写,而mod dir改变它,因此它被重写 制作重复的查询字符串,

任何想法?

编辑:我添加了Rewritelog的相关部分,这一切都来自一个请求:

strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^images\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^docs\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^pages\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies'
rewrite 'movies' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^images\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^docs\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^pages\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies/'
rewrite 'movies/' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies&page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]

访问日志中的相关部分:

"GET /movies HTTP/1.1" 301
"GET /movies/?page=movies HTTP/1.1" 200

3 个答案:

答案 0 :(得分:1)

您可以将重写规则更改为:

RewriteRule ^([A-Za-z0-9\_\-]+)/?$ /pages/pagehandler.php?page=$1 [L,QSA,NS]

请注意/之前的pages

答案 1 :(得分:1)

对于遇到此问题的任何人,我最终做的是禁用Directory Slash:

<IfModule mod_dir.c>
   DirectorySlash Off
</IfModule>

我使用RewriteRule来重定向所有目录请求,而不是以/

结尾

我猜这是mod_rewrite和mod_dir之间的某种碰撞 正如我主要想的那样

答案 2 :(得分:0)

  

我使用RewriteRule来重定向所有目录请求,而不是以/

结尾

我遇到了类似的问题,但找不到mod_rewrite的解决方案(不知道如何使用301重定向添加此斜杠 - 尝试了所有内容)。

我所做的是定义:

<IfModule mod_dir.c>
   DirectorySlash Off
</IfModule>
ErrorDocument 404 404.php

如果现在错过尾部斜杠,如果网址现在没有尾部斜杠,则使用404.php重定向客户端:

<?php 
$home="http://".$_SERVER['HTTP_HOST'];
$url=$home.$_SERVER['REQUEST_URI']; 
?><html>
<head>
<title><?php echo $_SERVER['REDIRECT_STATUS']; ?></title>
<?php if(!preg_match('/^.+\/$/', $url)) { ?>
<meta http-equiv="refresh" content="0; URL=<?php echo $url; ?>/">
<? } ?>
</head>
<body>
<h1><?php echo $_SERVER['REDIRECT_STATUS']; ?></h1>
</body>
</html>

它闻起来像地狱,但是有效......