Apache mod_rewrite 在 .htaccess 中工作,但在 apache conf 文件中不起作用

问题描述 投票:0回答:1

我在 .htaccess 中可以做的是获取

example.com/s1/s2/s3
并将它们作为 php 中
$_REQUEST
数组中的 3 个元素。 我如何将其翻译为在conf文件中正确工作?

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [NC,L,QSA]

会议

<IfModule mod_ssl.c>
<VirtualHost *:443>
        DocumentRoot "/home/...path.../public"
        ServerName example.com
        Alias /lib /home/...path.../otherlib/
        <Directory />
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>
        RewriteEngine on
        RewriteCond %{REQUEST_URI} !^/lib/ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php [NC,L,QSA]
</VirtualHost>
</IfModule>
apache mod-rewrite
1个回答
0
投票
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/lib/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [NC,L,QSA]

当在 virtualhost 上下文中使用时,mod_rewrite 会在请求映射到文件系统之前更早地处理。因此,

REQUEST_FILENAME
尚不包含请求的 URL 映射到的完整文件系统路径,它仅包含根相对 URL(与
REQUEST_URI
相同)。所以,这两个“否定”条件总是会成功。 (因此您的所有静态资源也将被重写到前端控制器。) 您需要使用前瞻,例如。

%{LA-U:REQUEST_FILENAME}

或从

DOCUMENT_ROOT
REQUEST_URI
构造文件名(在任一上下文中都有效)。例如:
RewriteCond %{REQUEST_URI} !^/lib/ [NC]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/. /index.php [L]

我还简化了 
RewriteRule

模式,因为无需在此处捕获 URL 路径或匹配根目录。

NC
QSA
标志也是不必要的。
    

© www.soinside.com 2019 - 2024. All rights reserved.