使用apache 2.4.23重写规则不能按预期工作

时间:2016-10-24 15:16:22

标签: php apache .htaccess mod-rewrite url-rewriting

我正在使用Virtual Box VM和Vagrant创建一个Ubuntu 14盒子,别无选择,只能将apache 2.4.23用作Web服务器。我的应用程序有一个前端控制器(index.php),它在路由方面处理网站的大部分工作,但也有一些用户可以访问的标准文件。

我的团队中还有其他开发人员仍然拥有apache 2.4.20,他们的盒子工作正常。

我的问题是重写规则没有按预期运行。这是升级到2.4.23之前的.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php/$1

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) /index.php/$1 [L]

    Options All -Indexes

</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

这是apache2.conf:

# Security
ServerTokens OS
ServerSignature On
TraceEnable On

ServerName "my-local"
ServerRoot "/etc/apache2"
PidFile ${APACHE_PID_FILE}
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15

User www-data
Group www-data

AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

<Directory /var/www/>
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>


HostnameLookups Off
ErrorLog "/var/log/apache2/error.log"
LogLevel warn
EnableSendfile Off

#Listen 80


Include "/etc/apache2/mods-enabled/*.load"
Include "/etc/apache2/mods-enabled/*.conf"
Include "/etc/apache2/ports.conf"

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combi$
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional "/etc/apache2/conf.d/*.conf"
IncludeOptional "/etc/apache2/sites-enabled/*"

只要我将R添加到重写规则中,我就可以访问该网站,但这是清除POST。我尝试使用GET作为一种快速解决方法,但是我的一些表单最终会出现对apache来说太长的请求字符串。

我需要知道这些配置中是否存在我无法看到的缺陷,或者是否存在我不知道的解决方法。

1 个答案:

答案 0 :(得分:0)

以下让我解决了这个问题:

  • 重新配置到Ubuntu 16
  • 在我的apache中添加了custom_fragment: /var/www/AudiMoc/htaccess config.yaml

在htaccess中:

## Custom fragment.
## This file can be refered to by the 'apache:' section config.yaml in case of a bug described by
## https://stackoverflow.com/questions/17095981/why-apache-mod-rewrite-rewrites-twice-my-url?rq=1.
## If you use it, then remove or rename /var/www/AudiMoc/.htaccess.
<IfModule mod_rewrite.c>
    RewriteEngine On
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php/$1
    #Without this, rewrite_mod will think that '/' matches the directory /var/www/AudiMoc/.
    RewriteRule ^/$ /index.php [L]
    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin
    ErrorDocument 404 /index.php
</IfModule>

apache2.4.23-5有一个错误,它使[R,L]成为必须的,而且我无法更轻松地绕过它的原因是因为我必须在虚拟盒子上拥有我的开发环境使用puphpet构建配置。 Puphpet.com在他们引用的存储库中只有一个版本的apache2,所以我无法绕过这个bug。

自定义片段让我解决了这个问题。