# problem with mod_rewrite [solved, seeking the reasons]

## pioneer-hg

I have a site running with CMS written in PHP and using mod_rewrite (SAPID: http://sapid.sourceforge.net).

All was fine until I decided to make a corporate web-server (on Gentoo, of course) and to move the site to this server. Before this I hosted the site on my local machine with Windows.

I moved the site to the server and I saw this:

/var/log/apache2/error_log:

```
[Thu Jun 09 10:41:10 2005] [error] [client 192.168.0.18] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'Rewrite Options MaxRedirects' to increase the limit if neccessary., referer: http://sapid.illichivska.com.ua/
```

I tried to put "Rewrite Options MaxRedirects = 100" to .htaccess and to commonapache.conf, but I have the same result.

I will be grateful for any advice on this question.

----------

## adaptr

Obviously, you have triggered some sort of recursive loop in the Redirect directive.

Won't know until you post the relevant parts of your config, of course.

----------

## pioneer-hg

An .htaccess file provided by CMS:

```
Options +Followsymlinks

DirectoryIndex index.php

ErrorDocument 404 /404/

Options +Followsymlinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?%{QUERY_STRING}
```

----------

## adaptr

Rewrites in per-directory .htaccess files work with the relative URL.

This means that the above rewriterule will, when applied, generate a URL that still points to the current directory - at which point the htaccess file will be processed again, and the rule applied, and the htacces file .. you get the idea.

Infinite loopy be there.

http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html

for all the dirty info.

----------

## pioneer-hg

Hmmmm .... but why do I have not any problems with exactly the same website (including provided with this CMS .htaccess) on my Windows machine? On Windows, I use auto-configured package Denwer (http://denwer.ru) which contain Apache, PHP and MySQL.

----------

## adaptr

Because the behaviour of mod_rewrite with per-directory htaccess files is configurable in php.ini.

As mentioned by the apache docs I pointed you to.

----------

## pioneer-hg

Excuse me, it seems I am a dummie, but I can not find in either Apache-2.0 documentation nor in PHP documentation any words about php.ini and mod_rewrite. So, I don't know how to adjust PHP configuration to work correctly.

----------

## adaptr

As the apache doc says, you can force mod_rewrite to do a complete redirect by prefixing your substitution URL with http://; it should jump out of the loop then.

I'm not entirely sure of how this stuff works myself, since simple it ain't.

My thoughts would be to avoid ambiguous rewrites as much as possible; turning it into a known-state machine for very large numbers of states is still preferable to allowing any substitution (and an infinite number of states).

But I am curious as to why you have not compared the configurations of the two systems - if one works then why not copy its config ?

----------

## rex123

Here's my interpretation (comments after relevant line):

DirectoryIndex index.php 

# If a request is made to a directory, redirect to index.php

RewriteEngine On

# Turns the engine on

RewriteCond %{REQUEST_FILENAME} !-f

# Only continue if the request maps to something that is not a regular file

RewriteCond %{REQUEST_FILENAME} !-d

# Only continue if the request maps to something that is not a directory

RewriteRule ^(.*)$ index.php?%{QUERY_STRING}

# Map everything (that isn't a file or a directory) to index.php

--

My guess is that index.php is not found, hence the loop. What happens if you comment out all the rewrite rules, and go to http://your.domain/index.php ?

I would guess a 404 error. Fix that, and you should be fine.

This is *only a guess*. There's no way I would bet on it, at any odds.

----------

## pioneer-hg

I have explored php.ini files on both machines and here are the differences:

```
Linux                               | Windows

memory_limit=64M                    | memory_limit=8M

error_reporting = E_ALL & ~E_NOTICE | error_reporting = E_ALL ; & ~E_NOTICE

*commented*                         | error_prepend_string = "<!--error-->"

*commented*                         | error_append_string = "<script language=JavaScript src='/errors__/phperror_js.php'></script>"

register_globals = On               | register_globals = Off

magic_quotes_gpc = On               | magic_quotes_gpc = Off

include_path = ".:/usr/lib/php"     | include_path = ".;/usr/local/php/PEAR"

extension_dir = /usr/lib64/php/ext-

ensions/no-debug-non-zts-2002-04-29 | extension_dir = "/usr/local/php/extensions"

*commented*                         | upload_tmp_dir = /tmp

allow_url_fopen = Off               | allow_url_fopen = On

*of course, commented*              | extension=php_curl.dll

*of course, commented*              | extension=php_gd2.dll

*of course, commented*              | extension=php_iconv.dll

SMTP = localhost                    | *commented*

smtp_port = 25                      | *commented*

*commented*                         | sendmail_path = \usr\sbin\sendmail.exe -t -i

*commented*                         | debugger.enabled=on

*commented*                         | debugger.profiler_enabled=on

*commented*                         | session.save_path = /tmp

session.use_trans_sid = 0           | session.use_trans_sid = 1

mbstring.func_overload = 7          | *commented*

*commented*                         | sockets.use_system_read = On
```

I do not want to just copy php.ini from Windows to Linux, I want to know exactly what is wrong.

Linux: Apache-2.0.52-r1, PHP and mod_php 4.3.11

Windows: Apache 1.3.31, PHP and mod_php 4.3.10

----------

## pioneer-hg

 *rex123 wrote:*   

> My guess is that index.php is not found, hence the loop. What happens if you comment out all the rewrite rules, and go to http://your.domain/index.php ?
> 
> I would guess a 404 error. Fix that, and you should be fine.
> 
> This is *only a guess*. There's no way I would bet on it, at any odds.

 

Thank you for your good explanation.

I commented out all strings in .htaccess (not only rewrite rules), and I have no errors. Both with original .htaccess and with commented there are such situation: the main page (that is handled by index.php) is displayed correctly, attempt to get every other page results with error.

----------

## rex123

I can't see how php.ini can *possibly* have anything to do with url rewriting in apache. That must be a red herring.

You might want to check that the other apache config files don't have any rewrite rules in them, as well (in case there are more things operating on this directory than just the .htaccess file).

My reading of the mod_rewrite docs suggests that you shouldn't get loops unless you specify options to restart the rewriting with the new URL. So I don't think I can be any more help.

----------

## adaptr

 *rex123 wrote:*   

> I can't see how php.ini can *possibly* have anything to do with url rewriting in apache. That must be a red herring.

 

Yes.. color me stupid  :Embarassed: 

My brain must have shut down somewhere.

----------

## pioneer-hg

I've looked through apache.conf and commonapache.conf and I didn't found any strings with "rewrite" except "LoadModule rewrite_module modules/mod_rewrite.so".

----------

## MrUlterior

 *adaptr wrote:*   

> Rewrites in per-directory .htaccess files work with the relative URL.
> 
> This means that the above rewriterule will, when applied, generate a URL that still points to the current directory - at which point the htaccess file will be processed again, and the rule applied, and the htacces file .. you get the idea.
> 
> Infinite loopy be there.
> ...

 

 *Quote:*   

> 
> 
> Linux: Apache-2.0.52-r1, PHP and mod_php 4.3.11
> 
> Windows: Apache 1.3.31, PHP and mod_php 4.3.10
> ...

 

 *pioneer-hg wrote:*   

> 
> 
> Hmmmm .... but why do I have not any problems with exactly the same website (including provided with this CMS .htaccess) on my Windows machine? On Windows, I use auto-configured package Denwer (http://denwer.ru) which contain Apache, PHP and MySQL.
> 
> 

 

I believe adaptr is correct in his first diagnosis of the problem; the reason for the different behaviour is simply the different version of apache. 2.x and its modules are a whole different breed to 1.x - if you want to compare this accurately, downgrade to 1.3.x and you should have the same behavior.

Meanwhile to solve this I think we need some critical pieces of information: firstly though you've stated you have an error in the log file, does it work?  :Smile:  Next, please post the <DirectoryIndex ... /> sections of your httpd.conf and the output of ls -la of the index.php, ls -lda of the directory that contains the index.php and of the root directory of your DirectoryIndex directive. And finally post the corresponding access_log entry for an attempt to access the page & an attempt that should be rewritten.

----------

## pioneer-hg

Sorry for delayed answer.

I have the same problem on another Gentoo machine with apache-1.3 - I think the problem is not with apache version.

Here are the files:

/etc/apache2/conf/vhosts (this file is included from apache2.conf):

```
UseCanonicalName Off

#

# this log format can be split per-virtual-host based on the first field

LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

CustomLog logs/access_log vcommon

#

# include the server name in the filenames used to satisfy requests

VirtualDocumentRoot /var/www/vhosts/%0/htdocs

VirtualScriptAlias  /var/www/vhosts/%0/cgi-bin
```

/etc/init.d/apache2/apache2.conf (extraction from this file):

```
DocumentRoot /var/www/localhost/htdocs
```

/etc/init.d/apache2/commonapache.conf (extraction from this file):

```
...

<IfModule mod_dir.c>

    DirectoryIndex index.html index.html.var index.php index.php3 index.shtml index.cgi index.pl index.htm Default.htm default.htm

</IfModule>

...

<Directory />

  Options -All -Multiviews

  AllowOverride None

  <IfModule mod_access.c>

    Order deny,allow

    Deny from all

  </IfModule>

</Directory>

...

<Directory /var/www>

#

# This may also be "None", "All", or any combination of "Indexes",

# "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews".

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

    Options All +ExecCGI

    AddDefaultCharset windows-1251

#

# This controls which options the .htaccess files in directories can

# override. Can also be "All", or any combination of "Options", "FileInfo",

# "AuthConfig", and "Limit"

#

    AllowOverride All

#

# Controls who can get stuff from this server.

#

    <IfModule mod_access.c>

      Order allow,deny

      Allow from all

    </IfModule>

</Directory>

...
```

ls output:

```
fileserv / # cd /var/www/vhosts/sapid.illichivska.com.ua/htdocs/

fileserv htdocs # pwd

/var/www/vhosts/sapid.illichivska.com.ua/htdocs

fileserv htdocs # ls -la index.php

-rwxrwxrwx  1 apache apache 12084 May 24 07:06 index.php

fileserv htdocs # ls -lda .

drwxrwxrwx  9 apache apache 824 Jun  9 10:22 .

fileserv htdocs #

```

access_log:

```
192.168.0.18 - - [10/Jun/2005:14:36:08 +0300] "GET /var/www/vhosts/sapid.illichivska.com.ua/htdocs/index.php HTTP/1.1" 302 379 "http://sapid.illichivska.com.ua/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
```

----------

## adaptr

```
192.168.0.18 - - [10/Jun/2005:14:36:08 +0300] "GET /var/www/vhosts/sapid.illichivska.com.ua/htdocs/index.php HTTP/1.1" 302 379 "http://sapid.illichivska.com.ua/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
```

That can't be right - an HTTP request never includes a filesystem location - it should include a URL, and a URL only!

How exactly are you trying to access this page ?

----------

## minskpower

 *adaptr wrote:*   

> 
> 
> That can't be right - an HTTP request never includes a filesystem location - it should include a URL, and a URL only!
> 
> How exactly are you trying to access this page ?

 

Maybe the vhost document root is configured in a wrong way?

Then again, this problem needs not be related to the apache server.

You can get redirection limit exceeded pretty easily if your code is not thoroughly checked.\

For instance: in index.php you are checking for connection to database, if it fails you set an error flag to the session and do a redirect to the same page where it would print the error. You are creating a loop here, since error will be encountered again and another redirect... or some includes do the same thing..

now if you are not using mod_rewrite, apache will not complain and loop hapily, and the page will seem to be loading forever without anything actualy happening.

Check the scripts for conditions like these, they are quite common, I must have stumbled dozens of times on this kind of errors.

----------

## pioneer-hg

 *adaptr wrote:*   

> How exactly are you trying to access this page ?

 

In normal way, for example - http://sapid.illichivska.com.ua/en/basic/

I think the root of this problem is in php scripts ...

----------

## pioneer-hg

Hello everybody, it works!

The solution is to change the string

```
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}
```

 in .htaccess to string 

```
RewriteRule ^(.*)$ /index.php?%{QUERY_STRING}
```

, i.e. change "index.php" to "/index.php".

Can everybody tell which is the reason of such behaviour of apache - how to determine which .htaccess is needed in certain case?

----------

## adaptr

You always need the last one.

The rewrite engine apparently strips off everything, including the root slash.

So a resulting URL would look like http://www.yoursite.comindex.php - which is not what you want.

Rather simple, really  :Wink: 

----------

## pioneer-hg

But, I performed a lot of installations of Apache+PHP with first variant of .htaccess, and it worked. So, it is not always necessary to put "/" before index.php, isn't it?

----------

## MrUlterior

Are your versions of PCRE libs exactly the same? When I have some time I might be able to check this out; but for now Im very busy with RAID problems

----------

