【问题标题】:mod_rewrite() too many internal redirectsmod_rewrite() 内部重定向太多
【发布时间】:2015-01-04 23:34:06
【问题描述】:

我已经成功实现了基本的 facebook like URL:

http://example.com/(username) 内部调用 http://example.com/sites/(username) 使用以下 mod_rewrite 代码:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]

请注意,(用户名)文件夹存在于站点文件夹中

上面的工作非常好。但现在我想美化我的其他网址,例如: http://example.com/(username)/Image/2/ 这应该在内部调用 http://example.com/sites/(username)/index.php?type=image&id=2

为此,我在倒数第二行添加了以下代码:

RewriteRule ^([^/]+)/Image/(.*)$ /sites/$1/index.php?type=image&id=$2 [NC,L]

我希望最后的 [L] 将阻止在此之后计算规则。但是从日志中发现它进入了一个无限循环并被截断。

有人能指出我在这里做错了什么吗?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    在您的 DocumentRoot 文件夹中,您可以拥有这个 .htaccess:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^([^/]+)/([^/]+)/(\d+)/?$ sites/$1/index.php?type=$2*id=$3 [QSA,L]
    
    RewriteRule ^((?!sites[^/]+)(/.*)?$ sites/$1$2 [NC,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多