apache htaccess 文件、php、包含目录和 windows XAMPP 配置噩梦

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:47:11  来源:igfitidea点击:

htaccess files, php, includes directories, and windows XAMPP configuration nightmare

phpapache.htaccessxamppconfig

提问by Josh Smeaton

XAMPP makes configuring a local LAMP stack for windows a breeze. So it's quite disappointing that enabling htaccess files is such a nightmare.

XAMPP 使为 Windows 配置本地 LAMP 堆栈变得轻而易举。因此,启用 htaccess 文件是一场噩梦,这令人非常失望。

My problem:
I've got a PHP application that requires apache/php to search for an /includes/ directory contained within the application. To do this, htaccess files must be allowed in apache and the htaccess file must specify exactly where the includes directory is.

我的问题:
我有一个 PHP 应用程序,它需要 apache/php 来搜索应用程序中包含的 /includes/ 目录。为此,必须在 apache 中允许 htaccess 文件,并且 htaccess 文件必须准确指定包含目录的位置。

Problem is, I can't get my apache config to view these htaccess files. I had major hassles installing this app at uni and getting the admins there to help with the setup. This shouldn't be so hard but for some reason I just can't get apache to play nice.

问题是,我无法让我的 apache 配置查看这些 htaccess 文件。我在 uni 安装这个应用程序并让管理员在那里帮助设置时遇到了很大的麻烦。这不应该那么难,但出于某种原因,我无法让 apache 发挥良好的作用。

This is my setup:

这是我的设置:

c:\xampp
c:\xampp\htdocs
c:\xampp\apache\conf\httpd.conf - where I've made the changes listed below
c:\xampp\apache\bin\php.ini - where changes to this file affect the php installation
It is interesting to note that c:\xampp\php\php.ini changes mean nothing - this is NOT the ini that affects the php installation.

c:\xampp
c:\xampp\htdocs
c:\xampp\apache\conf\httpd.conf - 我做了下面列出的更改
c:\xampp\apache\bin\php.ini - 对这个文件的更改影响 php 安装
有趣的是,c:\xampp\php\php.ini 更改没有任何意义 - 这不是影响 php 安装的 ini。

The following lines are additions I've made to the httpd.conf file

以下几行是我对 httpd.conf 文件所做的添加

#AccessFileName .htaccess
AccessFileName htaccess.txt
#
# The following lines prevent .htaccess and .htpasswd 
# files from being viewed by Web clients. 
#
#<Files ~ "^\.ht">
<Files ~ "^htaccess\.">
    Order allow,deny
    Deny from all
</Files>

<Directory "c:/xampp/htdocs">
#
# AllowOverride controls what directives may be placed in 
# .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All
#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all
</Directory>

The following is the entire htaccess file contained in: c:\xampp\htdocs\application\htaccess.txt

以下是整个htaccess文件包含在:c:\xampp\htdocs\application\htaccess.txt

<Files ~ "\.inc$">
  Order deny,allow
  Deny from all
</Files>

php_value default_charset "UTF-8"
php_value include_path ".;c:\xampp\htdocs\application\includes"
php_value register_globals 0
php_value magic_quotes_gpc 0
php_value magic_quotes_runtime 0
php_value magic_quotes_sybase 0
php_value session.use_cookies 1
php_value session.use_only_cookies 0
php_value session.use_trans_sid 1
php_value session.gc_maxlifetime 3600
php_value arg_separator.output "&amp;"
php_value url_rewriter.tags "a=href,area=href,frame=src,input=src,fieldset="  

The includes directory exists at the location specified.

包含目录位于指定的位置。

When I try to access the application I receive the following error:

当我尝试访问该应用程序时,我收到以下错误:

Warning: require(include.general.inc) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\application\menu\logon.php on line 21

Fatal error: require() [function.require]: Failed opening required 'include.general.inc' (include_path='.;C:\xampp\php\pear\random') in C:\xampp\htdocs\application\menu\logon.php on line 21

致命错误:require() [function.require]:在 C:\xampp\htdocs\application\ 中打开所需的“include.general.inc”(include_path='.;C:\xampp\php\pear\random')失败第 21 行的 menu\logon.php

The include path c..\random\ is specified in the php.ini file listed above. The xampp install fails to allow another include path as specified in the htaccess.txt file.

包含路径 c..\random\ 在上面列出的 php.ini 文件中指定。xampp 安装无法允许 htaccess.txt 文件中指定的另一个包含路径。

Please help! I'm guessing there's probably an error with the httpd.conf OR the htaccess.txt file.. but it doesn't seem to be reading the htaccess file. I've tried to be as thorough as possible so forgive the verbosity of the post.

请帮忙!我猜 httpd.conf 或 htaccess.txt 文件可能有错误..但它似乎没有读取 htaccess 文件。我试图尽可能彻底,所以请原谅这篇文章的冗长。

Now I know a workaround could be to simply add the include_path to the php file, but I'm not going to have access to the php.ini file on the location I plan to deploy my app. I will, however, be able to request the server admin allows htaccess files.

现在我知道一个解决方法可能是简单地将 include_path 添加到 php 文件,但我将无法访问我计划部署我的应用程序的位置上的 php.ini 文件。但是,我将能够请求服务器管理员允许 htaccess 文件。

Update: renamed htacces.txt to .htaccess and ammended the appropriate directives in the httpd.conf file and all seems to work. The application suggested the naming of htaccess.txt and the ammended directives in the httpd. These are obviously wrong (at least for a xampp stack). Thanks for the help.

更新:将 htacces.txt 重命名为 .htaccess 并修改了 httpd.conf 文件中的相应指令,一切似乎都有效。该应用程序建议将 htaccess.txt 命名为 httpd 中的修改指令。这些显然是错误的(至少对于 xampp 堆栈而言)。谢谢您的帮助。

By the way, using ini_set() is a much friendlier way if the app needs to be deployed to multiple locations so thanks especially for that pointer.

顺便说一句,如果应用程序需要部署到多个位置,使用 ini_set() 是一种更友好的方式,所以特别感谢那个指针。

回答by Till

  1. Why do you need to rename .htaccess to htaccess.txt
  2. Try setting the include_path using set_include_path()and see if that helps (as an intermediate fix)
  3. Verify which php.ini to use through a phpinfo()
  1. 为什么需要将 .htaccess 重命名为 htaccess.txt
  2. 尝试使用set_include_path()设置 include_path并查看是否有帮助(作为中间修复)
  3. 通过phpinfo()验证要使用哪个 php.ini

回答by Lucas Oman

You can alter the include_path on each request using ini_set(). This would avoid having to use htaccess at all.

您可以使用 ini_set() 更改每个请求的 include_path。这将完全避免使用 htaccess。

回答by Rich Bradshaw

My htaccess works under XAMPP fine - though some modules are disabled by default - are you sure that the modules you want are enabled?

我的 htaccess 在 XAMPP 下工作正常 - 尽管默认情况下禁用了某些模块 - 您确定启用了您想要的模块吗?

回答by SchizoDuckie

I think the searchprase you are looking for is:

我认为您正在寻找的 searchprase 是:

AllowOverride All

允许覆盖所有

My guess is that within xampp you need to enable AllowOverride (through an .htaccess) in httpd.conf. It's a simple security measure that prevents newbies from installing a hackable platform :P

我的猜测是,在 xampp 中,您需要在 httpd.conf 中启用 AllowOverride(通过 .htaccess)。这是一种简单的安全措施,可以防止新手安装可破解的平台:P

回答by f13o

You need to enable AllowOverride All in main http.conf file. Look inside XAMPP_DIR/apache/conf/http.conf)

您需要在主 http.conf 文件中启用 AllowOverride All。看看里面 XAMPP_DIR/apache/conf/http.conf)

回答by Deepak

Simply Do this:

只需这样做:

open file ...\xampp\apache\conf\httpd.conf

find line "LoadModule rewrite_module modules/mod_rewrite.so"

if here is a # before this line remove it.(remove comment)

its working

它的工作