Friday, June 8, 2012

mod_rewrite/GoDaddy problem


Ok, so I really don't know much about mod_rewrite and I'm looking over the apache docs and still not figuring this out.



Here is my htaccess (which is mostly just copy & pasted from a site I found): .htaccess in base dir:




<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>



.htaccess in /public dir:




<IfModule mod_rewrite.c>
RewriteEngine On

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

RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

</IfModule>



Basically I'm using the above htaccess files on my current test server and it works great and exactly like I would expect (everything gets seamlessly redirected to public dir).



Now when I throw this on my GoDaddy hosting account i get "Internal Server Errror". I've done some searching and I'm fairly certain I should be able to use mod_rewrite with GoDaddy.



I suspect this is because I'm not using the base directory to host the site in. The site is in in the folder html/myapp/ (where html is the base directory) and i have a subdomain set up in GoDaddy to look in that folder.



Edit:



After checking the error logs I see this-




[Wed Apr 21 17:59:45 2010] [error] [client] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.



So I assume I've created some sort of infinite loop? But I'm not sure how...


Source: Tips4all

3 comments:

  1. Ok I got it working. heres what I did:

    for the .htaccess in the root directory:

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^$ /public/ [L]
    RewriteRule (.*) /public/$1 [L]
    </IfModule>


    and for the one in /public:

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

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

    RewriteRule ^(.*)$ /public/index.php?url=$1 [PT,L]

    </IfModule>


    I'm honestly not sure why this works, but it does. Thanks to all that helped.

    ReplyDelete
  2. Have you tried:

    Options +FollowSymlinks
    RewriteEngine on


    I remember having trouble getting mod_rewrite working on godaddy.

    Instead of using [L] try using [R=301,L]. R=301 means redirect with status code 301 (Moved permanently). If you want to see the other status codes see here. If you don't actually redirect, the .htaccess on the root directory would get called repeatedly.

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [R=301,L]
    RewriteRule (.*) public/$1 [R=301,L]
    </IfModule>


    The way you had it wouldn't send people to the public directory. And I believe that it is looping.

    Edit: This should explain the looping.

    Edit 2: Instead of (.*) try using ^(.*)$. Other than that, I'm somewhat at a loss.

    ReplyDelete
  3. The "Internal Server Error" makes me think that GoDaddy might have left mod rewrite turned off for your host. Its happened to me at least a couple times on different hosting companies. I would double check with their support first before wasting too much time on it, just to be on the safe side.

    You should also check to see if they provide any form of apache error logging. If you have access to the apache log it should be fairly simple to determine the source problem.

    Edit: I got the infinite loop deal when I tried it locally as well. I think you need to add a conditional to stop that from happening. This seemed to work on my machine:

    RewriteCond %{REQUEST_URI} !public
    RewriteRule (.*) public/$1 [L]

    ReplyDelete