大网数据广告活动
服务器运维问答

.htaccess RewriteRule not working

· 52CE 我爱测

I have a windows environment using WAMP where this works fine. I am just setting up my Ubuntu desktop development environment, and this particular mod_rewrite is not working. It's pretty basic, but if I go to example.local/files/…

问题原文

原帖链接 · Chad · CC BY-SA 4.0

I have a windows environment using WAMP where this works fine. I am just setting up my Ubuntu desktop development environment, and this particular mod_rewrite is not working.

It's pretty basic, but if I go to
example.local/files/a-real-file.pdf I get a 404 not found from apache. If I go to
example.local/files.php/a-real-file.pdf the file will display, as expected. If I go to
example.local/files.php/non-existing-file.who.cares I get a var_dump($thelink) as expected from my files.php logic.

the .htaccess in my web root

RewriteEngine On
RewriteRule ^files/(.*)$ files.php/$1

my apache vhost config

<VirtualHost *:80>
    ServerName reqapp.localhost
    DocumentRoot /var/www/html/aaApp/public_html
    <Directory /var/www/html/aaApp/public_html/>
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

and for good measure, the "localhost" conf

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

as well as the "applicable" part of apache2.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

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

according to phpinfo() mod_rewrite is loaded. If a create a syntax error in the .htaccess file, the site does crash.

I don't know where else to look. I can't fine any other .conf files in and of the "{mods|conf|sites}-enabled" files that should be effecting this directory.

It might be worth mentioning that I also have a Joomla instance running, and the SEF is working fine with it.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Options +Indexes +Includes +FollowSymLinks +MultiViews

You need to disable MultiViews (or simply not enable it). For example:

Options +Indexes +Includes +FollowSymLinks -MultiViews

Or remove it altogether from the Options directive if it is has not already been enabled on the server.

With MultiViews enabled, mod_negotiation will trigger an internal subrequest for files.php (without the additional pathname information that is required for your request to be successful) before mod_rewrite gets to test the request. So the RewriteRule pattern never matches and the rewrite does not occur.

In detail...

MultiViews is part of mod_negotiation (content-negotiation). The issue in your case is that you are requesting /files/... (in a valid directory - the document root) and a file with that basename (ie. files) exists in that directory. If you were to request /bar/... (which presumably does not exist) then you would not have experienced this problem.

When you requested /files/..., MultiViews results in Apache searching for an appropriate resource (files.* - essentially trying various file extensions) until a response with the expected mime-type is found. The "problem" is that this happens very early in the request, so will often conflict with mod_rewrite.

MultiViews is a simple way of enabling extensionless URLs (no URL rewriting is required). However, as soon as you need to do something a little more complex then it will likely conflict with mod_rewrite - a common problem. MultiViews is not enabled on Apache by default (however, some shared hosts do enable it).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。