Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess规则-重定向太多_Apache_.htaccess_Redirect_Mod Rewrite - Fatal编程技术网

Apache .htaccess规则-重定向太多

Apache .htaccess规则-重定向太多,apache,.htaccess,redirect,mod-rewrite,Apache,.htaccess,Redirect,Mod Rewrite,我试图用htaccess文件完成两件事: 在除2个目录外的所有目录上强制使用https,仅当从prod url访问时(在开发人员上没有https) 重定向index.php中不存在的url 这就是我所拥有的: RewriteEngine On # Redirect all http to https RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} myserver\.com$ [NC] RewriteRule ^(api|images)($|

我试图用htaccess文件完成两件事:

  • 在除2个目录外的所有目录上强制使用https,仅当从prod url访问时(在开发人员上没有https)
  • 重定向index.php中不存在的url
  • 这就是我所拥有的:

    RewriteEngine On
    # Redirect all http to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} myserver\.com$ [NC]
    RewriteRule ^(api|images)($|/) - [L]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # The following rule tells Apache that if the requested filename
    # exists, simply serve it.
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    # The following rewrites all other queries to index.php. The 
    # condition ensures that if you are using Apache aliases to do
    # mass virtual hosting, the base path will be prepended to 
    # allow proper resolution of the index.php file; it will work
    # in non-aliased environments as well, providing a safe, one-size 
    # fits all solution.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
    

    当我添加第一位(https强制)时,我得到“太多重定向”错误。知道为什么吗?

    我认为这部分应该更像:

    RewriteRule ^(api|images)($|/) - [L]
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} myserver\.com$ [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    RewriteCond
    仅适用于下一个
    RewriteRule
    http->https
    的重定向规则是无条件的,将导致重定向循环

    这样做:

    RewriteEngine On
    
    # Redirect all http to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} myserver\.com$ [NC]
    RewriteCond %{THE_REQUEST} !\s/+(?:api|images)[/?\s] [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # The following rule tells Apache that if the requested filename
    # exists, simply serve it.
    
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # The following rewrites all other queries to index.php. The 
    # condition ensures that if you are using Apache aliases to do
    # mass virtual hosting, the base path will be prepended to 
    # allow proper resolution of the index.php file; it will work
    # in non-aliased environments as well, providing a safe, one-size 
    # fits all solution.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    
    RewriteRule ^ %{ENV:BASE}index.php [L]
    

    谢谢你的解释!这非常有效。