htaccess:将重定向规则转换为ModRewrite规则

时间:2013-09-27 09:35:31

标签: regex apache .htaccess mod-rewrite redirect

我应该转换我的htaccess文件中仍然存在的一些Redirect 301规则,因为我被告知我最好不要在同一个文件中混合使用Redirect和Mod Rewrite规则。

Redirect 301 /bar.php http://www.foo.com/abc/bar.php 
RedirectMatch 301 ^/(bar(?!bapapa\.)[^/.]+\.php)$ http://www.foo.com/abc/$1
  1. 将对/bar.php的每次访问重定向到子文件夹abc
  2. 中的同一文件
  3. 将对/bar开头的php文件(不包括/barbapapa.php)的每次访问重定向到子文件夹abc中的相同文件
  4. 我无法转换这两条规则。 至于第一条规则,我试过:

    RewriteRule ^bar.php$ "http://www.foo.com/abc/bar.php" [R=301]
    

    我被正确地从/bar.php重定向到/abc/bar.php,但是我没有看到浏览器消息(Firefox),而是说明服务器正在以请求无法完成的方式重定向。 任何提示?感谢

    编辑:/abc/.htaccess的内容

    RewriteEngine On
    
    #inherit from root htaccess and append at last
    RewriteOptions inherit
    
    #disable hotlinking but allow image bots
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?foo.com [NC]
    RewriteCond %{HTTP_REFERER} !google\. [NC]
    RewriteCond %{HTTP_REFERER} !images.google\.
    RewriteCond %{HTTP_REFERER} !yahoo\.
    RewriteCond %{HTTP_REFERER} !bing\.
    RewriteCond %{HTTP_REFERER} !msn\.
    RewriteCond %{HTTP_REFERER} !ask\.
    RewriteCond %{HTTP_REFERER} !arianna\.
    RewriteCond %{HTTP_REFERER} !yandex\.
    RewriteCond %{HTTP_REFERER} !babylon\.
    RewriteCond %{HTTP_REFERER} !virgilio\.
    RewriteCond %{HTTP_REFERER} !avg\.
    RewriteCond %{HTTP_REFERER} !delta-search.com\.
    RewriteCond %{HTTP_REFERER} !images.search.conduit.com\.
    RewriteCond %{HTTP_REFERER} !search.findeer.com\.
    RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
    RewriteCond %{HTTP_REFERER} !search\/cache [NC]
    RewriteCond %{HTTP_REFERER} !cache [NC]
    RewriteRule \.(jpe?g|png|gif)$ - [NC,F]
    
    #rename old gif maps to png
    Options +FollowSymLinks
    RewriteRule ^([^.]+)\.gif$ http://www.foo.com/abc/$1.png [L,R=301]
    
    Options -Indexes
    

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+abc/ [NC]
RewriteRule ^(bar(?!bapapa\.)[^/.]*\.php)$ /abc/$1 [L,NC,R=302]