Getting Error 500 with this .htaccess on Linux, works fine on Windows

妖精的绣舞 提交于 2019-12-11 11:57:24

问题


I'm using XAMPP on Windows and LAMPP on Linux (Ubuntu) to develop in PHP locally. I have this in my .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

As you can see I don't want to type the extensions of filenames that end with either HTML or PHP. My both .htaccess contains same above content.

Amazingly, when I am requesting any file (or directory) on Ubuntu (even if with full extension), I'm getting Error 500. If I remove this .htaccess, everything gets well.

Everything is working as expected on Windows.

What's the problem?


回答1:


The last line is:
[Thu Sep 06 20:14:25 2012] [alert] [client 127.0.0.1] /opt/lampp/htdocs/.htaccess: Invalid command '\xef\xbb\xbfRewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Those characters (\xef\xbb\xbf) are the unicode byte order mark for UTF-8 and apache thinks it's garbage characters (on linux at least). In windows, this BOM is used to let Windows know that the file is encoded as UTF-8 instead of whatever default windows text file uses (I think it's UTF-16, little endian). You just need to go use your favorite linux text editor, and delete those characters. Depending on what editor you use, they may not even show up, so you may need to do something like "select from the end of the word RewriteEngine to the beginning of the line, and delete selection, then, from the beginning of the line, just type RewriteEngine by hand.

There's a chance that the htaccess file won't work properly once you move it back to Windows after removing the BOM, I'm not really sure.



来源:https://stackoverflow.com/questions/12299684/getting-error-500-with-this-htaccess-on-linux-works-fine-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!