A Magento Redirect Loop

Discuss practical ways rearrange URLs using mod_rewrite.

A Magento Redirect Loop

Postby AndrewKalonga » Fri May 22, 2009 8:00 am

I've been having a problem with a redirecting loop lately. Can you help with mod_rewriting?

The problem started with the use of my own pages to handle product display. I haven't got exprience in creating dynamic pages using Magento CMS and hacking. As this process is not a quick and easy learning curve, I decided to use my pages side by side with Magento. Thus, I redirect requests to '(Magento's) index.php' to my 'index' - a php5 file without an extension, if the request url has no parameters. My permanent home is now 'index'. I have other php files eg 'shop' which layout products for different sections - women, men..

The setup works ok but two things are worrying me:
1- The redirect loops (shown in the error_log) for every request sent are just a resource overhaul
2- The site slowed down. Not all the images load.Tried it only on my PC, so could be wrong

So the problem as far as I know is that I have got the rulesets mixed up creating a loop. How can I correct the indefinite loop created n my .htaccess?

Code: Select all
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mysite.com/index/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.php/
RewriteRule ^(.*)index.php/$ http://www.mysite.com/index/$1 [R=301,L]


############################################
## workaround for HTTP authorization
## in CGI environment

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} -l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby richardk » Fri May 22, 2009 8:08 am

What are the URLs? Where are they supposed to go?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php/?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php/?$ http://www.mysite.com/index/$1 [R=301,L]

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule !^(media|skin|js)(/.*)?$ /index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AndrewKalonga » Fri May 22, 2009 8:32 am

Here are the urls:
My pages:
http://www.integritytextiles.co.uk/index/
http://www.integritytextiles.co.uk/index/about
http://integritytextiles.co.uk/shop/women

Magento's:
http://integritytextiles.co.uk/catalog/ ... p/product/
http://integritytextiles.co.uk/shop-wom ... f0014.html
http://integritytextiles.co.uk/checkout/cart/


Your rewritng works the same way but I'm still getting the loop. See log below.

Code: Select all
MAIN error_log:
--------------------------------------------------------------------------------

[Fri May 22 10:23:30 2009] [error] [client 84.102.76.189] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:32 2009] [error] [client 79.92.106.15] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:32 2009] [error] [client 79.92.106.15] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:33 2009] [error] [client 196.201.90.36] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:33 2009] [error] [client 196.201.90.36] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:33 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:33 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:34 2009] [error] [client 209.143.112.12] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:34 2009] [error] [client 209.143.112.12] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:36 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:36 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:39 2009] [error] [client 84.153.224.205] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:39 2009] [error] [client 84.153.224.205] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:40 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:40 2009] [error] [client 212.179.68.113] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://freevideolectures.com/
[Fri May 22 10:23:42 2009] [error] [client 201.40.135.116] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri May 22 10:23:42 2009] [error] [client 201.40.135.116] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby richardk » Fri May 22, 2009 8:34 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(index|shop)(/.*)?$ - [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule !^(media|skin|js)(/.*)?$ /index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]


What is the purpose of
Code: Select all
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mysite.com/index/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.php/
RewriteRule ^(.*)index.php/$ http://www.mysite.com/index/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AndrewKalonga » Fri May 22, 2009 8:59 am

Code: Select all
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mysite.com/index/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.php/
RewriteRule ^(.*)index.php/$ http://www.mysite.com/index/$1 [R=301,L]


As I said, I redirect requests to index.php without parameters to my home page which is 'index'. So that http://www.integritytextiles.co.uk/index.php and http://www.integritytextiles.co.uk/index.php/ go to http://www.integritytextiles.co.uk/index/. Didn't know how to write that in one rule.

But then magento's urls still work e.g. http://integritytextiles.co.uk/checkout/cart/. This is using index.php file according to
Code: Select all
RewriteRule .* index.php [L]
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby AndrewKalonga » Fri May 22, 2009 9:03 am

Sorry, just to clarify. The 'index' is a filename not a a directory in
Code: Select all
integritytextiles.co.uk/index/
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby AndrewKalonga » Fri May 22, 2009 9:35 am

I've had a chat with someone from my Host Support. He says the redirects are not caused by my mod_rewrites. Should I take for fact or still tweak my rewrites? Which rewrite is the best?
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby AndrewKalonga » Fri May 22, 2009 9:43 am

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(index|shop)(/.*)?$ - [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule !^(media|skin|js)(/.*)?$ /index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]


@Richard - can you please explain this code. What's the difference with the one wrote before. They both work though.
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Postby richardk » Sat May 23, 2009 4:01 pm

I've had a chat with someone from my Host Support. He says the redirects are not caused by my mod_rewrites.

The /index/something requests are most likely matching the last rule and then index.php is doing the redirecting. If it didn't match it might not redirect. That is the purpose of
Code: Select all
# Ignore requests to /index, /index/*, /shop and /shop/*.
RewriteRule ^(index|shop)(/.*)?$ - [L]


Which rewrite is the best?

The mod_rewrite you posted looks wrong
Code: Select all
############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} -l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

but
Code: Select all
    RewriteCond %{REQUEST_FILENAME} -f

means only continue if it does match an existing file. All three together means to continue the request must match a file, directory and symbolic link at the same time.

I would try/use
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to /index, /index/*, /shop and /shop/*.
RewriteRule ^(index|shop)(/.*)?$ - [L]

# Redirect to remove an trailing /index.php or /index.php/.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php/?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php/?$ http://www.mysite.com/index/$1 [R=301,L]

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# If the request does not match an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and the request does not match an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# and the request does not match a symbolic link
RewriteCond %{SCRIPT_FILENAME} !-l
# and the request is not for /media/*, /skin/* or /js/* send
# the request to index.php.
RewriteRule !^(media|skin|js)(/.*)?$ /index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AndrewKalonga » Sun Aug 02, 2009 8:54 pm

Sorry for disappearing for a while. I took the rewrite and used it and it worked fine.

Now the next step is to hide the home page file name (index - without the extension) in the url; mysite.com/index. I have tried several ideas by piecing things together but only certain urls will work at one time.

What I want:
1- My home page should look like www.mysite.com not www.mysite.com/index
2- Still have index.php (withour query string) and index.php/ rewritten to index
3- Still have all requests redirected to index.php if they are not calling a file in media|skin|js folders
Code: Select all
RewriteRule !^(media|skin|js)(/.*)?$ /index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]

4- Redirect all requests to php files on root directory to /index/{requested file name without the .php}
Thus something like: www.mysite.com/terms calls www.mysite.com/index/terms. [where; index is the DirectoryIndex file and terms is terms.php]
Code: Select all
RewriteCond %{SCRIPT_FILENAME}\.php -f
RewriteRule ^(.*)$ /index/$1.php


Basically, my configuration has two index files (index and index.php) serving different functions. index is used for my custom front end (pages) while index.php is for Magento.

I will be very grateful for any quick assistance since I'm running late on a project.
AndrewKalonga
 
Posts: 10
Joined: Thu May 21, 2009 11:24 am
Location: Nottingham, UK

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 21 guests

cron