Too_many_redirects when using .htaccess

apache

#1

Hi,

We’re getting a too_many_redirects error in the browser when we use our own .htaccess file. It works fine on our server.

RewriteEngine On                                                                                                                              
# Redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]                                                                                                                                              
# Show ad                                                                                                                                     
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html		index.php?view=showad&adid=$7&cityid=$1 [QSA]
                                                                                                                                              
# Ads                                                                                                                                         
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/page([0-9]*)\.html			index.php?view=ads&catid=$3&subcatid=$5&cityid=$1&page=$7 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?					index.php?view=ads&catid=$3&subcatid=$5&cityid=$1 [QSA]
                                                                                                                                              
## Begin Version 5.6.2 - Pagination bug fix + Uniform page links ##

# Ads by cat                                                                                                                                  
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/page([0-9]*)\.html            index.php?view=ads&catid=$3&subcatid=0&&cityid=$1&page=$5 [L,QSA]

RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?								index.php?view=ads&catid=$3&cityid=$1 [QSA]

## End Version 5.6.2 - Pagination bug fix + Uniform page links ##
                                                                                                                               
# Show event                                                                                                                                  
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/events(/(([0-9]+)-([0-9]+)-([0-9]+)))?/([0-9]+)([-_][^/]*)?\.html			index.php?view=showevent&date=$4&adid=$8&cityid=$1 [QSA]
                                                                                                                                              
# Events on date                                                                                                                              
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/events/(([0-9]+)-([0-9]+)-([0-9]+))/page([0-9]*)\.html				index.php?view=events&date=$3&cityid=$1&page=$7 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/events/(([0-9]+)-([0-9]+)-([0-9]+))							index.php?view=events&date=$3&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/events										index.php?view=events&cityid=$1 [QSA]
                                                                                                                                              
# Show image                                                                                                                                  
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/images/([^/]+)/([0-9]+)([-_][^/]*)?\.html						index.php?view=showimg&posterenc=$3&imgid=$4&cityid=$1 [QSA]
                                                                                                                                             
# Images by user, paged                                                                                                                       
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/images/([^/]+)/page([0-9]+)\.html							index.php?view=imgs&posterenc=$3&cityid=$1&page=$4 [QSA]
                                                                                                                                              
# Top Images, paged                                                                                                                           
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/images/page([0-9]+)\.html								index.php?view=imgs&cityid=$1&page=$3 [QSA]
                                                                                                                                              
# Images by user                                                                                                                              
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/images/([^/]+)									index.php?view=imgs&posterenc=$3&cityid=$1 [QSA]
                                                                                                                                              
# Top Images                                                                                                                                  
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/images										index.php?view=imgs&cityid=$1 [QSA]
                                                                                                                                              
# Quick ad/event/image                                                                                                                        
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/showpost/([0-9]+)([-_][^/]*)?\.html							index.php?view=showad&adid=$3&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/showevent/([0-9]+)([-_][^/]*)?\.html							index.php?view=showevent&adid=$3&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/showimage/([0-9]+)([-_][^/]*)?\.html							index.php?view=showimg&imgid=$3&cityid=$1 [QSA]
                                                                                                                                              
# City/Region                                                                                                                                 
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?											index.php?view=main&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?											index.php?view=main&cityid=$1 [QSA]

## Begin Version 5.0 ##

# RSS URLs
RewriteRule ^feed/posts                           													rss.php?view=ads&cityid=0 [QSA]
RewriteRule ^feed/events                           													rss.php?view=events&cityid=0 [QSA]
RewriteRule ^feed/([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?			rss.php?view=ads&catid=$3&subcatid=$5&cityid=$1 [QSA]
RewriteRule ^feed/([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?								rss.php?view=ads&catid=$3&cityid=$1 [QSA]
RewriteRule ^feed/([-]?[0-9]+)([-_][^/]*)?/posts													rss.php?view=ads&cityid=$1 [QSA]
RewriteRule ^feed/([-]?[0-9]+)([-_][^/]*)?/events/(([0-9]+)-([0-9]+)-([0-9]+))						rss.php?view=events&date=$3&cityid=$1 [QSA]
RewriteRule ^feed/([-]?[0-9]+)([-_][^/]*)?/events													rss.php?view=events&cityid=$1 [QSA]

## End Version 5.0 ##

## BEGIN account mod

RewriteRule ^account.html$  /index.php?view=userpanel
RewriteRule ^account.html&action=(.*)$  /index.php?view=userpanel&action=$1
RewriteRule ^login.html$  /index.php?view=login
RewriteRule ^signup.html$  /index.php?view=signup

## END account mod

I think this is because it’s fighting with c9’s internal .htaccess. Any suggestions?


#2

The problem was the first few lines, which I’d added to redirect to https:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I suppose c9 tunnels an http connection through their own TLS cert, and forces the internal connection to use HTTP, thus causing a redirect loop.

Commenting out those lines solved the problem for me.


#3

So removing them seems to be your solution. But not a real solution

I want my htaccess to work on both my own server and the cloud 9 server.

Removing these lines will just break it again on my server


#4

I think I just kept two versions of the file in the two environments, but there might be a way to read an environment variable inside the .htaccess.


#5

You may be able to use a solution like I described in the following post:

I’m unfamiliar with how to reverse proxy with Apache involved, but if you can figure it out, it might allow you to configure Apache with HTTPS, but serve HTTP to the c9 proxy, so that you can easily deploy to production without needing to change anything.