Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

.htaccess - htaccess causing server error 500

I have a Zend Framework application with a .htaccess file in the root directory, directing all traffic to the /public directory, where index.php resides.

This is the contents of the .htaccess:

RewriteEngine On

RewriteRule ^.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/(upload|img|js|css|less)/.*$
RewriteCond %{DOCUMENT_ROOT}/public/upload%{REQUEST_URI} -f
RewriteRule ^(.+.(jpg|png|mp3))$ /public/upload%{REQUEST_URI} [NC,L]

;RewriteCond %{REQUEST_URI} !^/public/.*$
;RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]

As you can see, there are two commented out lines.

The section, supposedly directing traffic to /public, and which works on my local servers and my shared host, seems to fail miserably on my client's server.

So, with the two lines commented out, when I type the site's address in the browser, I get a directory listing. When I append /public to the address, I'm taken to /public/index.php, as expected, but since the request wasn't redirected correctly, neither are the ones to the JS and CSS files.

When I uncomment the lines, I get a 500 internal server error (index.php is not reached at all).

The other rewrites work correctly (at least as far as I can tell).

Any ideas why this would happen and how to correct this?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Save yourself a lot of grief and do the following:

  1. Set the document root to the public directory.
  2. Set the rewrite rules in the VirtualHost entry or use the standard Zend Framework rewrite rules in a .htaccess file in the public directory.

Ok, given your shared hosting constraints, try this one out...

  1. Place all the public files into your document root including the .htaccess file mentioned in step #2 above
  2. Place the rest of the folders (ie application, library) anywhere in the filesystem, preferably outside the web document root. An example might be /home/user/apps/my-app-name/
  3. Modify the index.php file with the new APPLICATION_PATH, eg

    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', 'home/user/apps/my-app-name/application');
    

Now everything should "just work" and your site won't be accessed with the word "public" in the URL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...