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

yv5phkfx  于 2023-10-23  发布在  Apache
关注(0)|答案(1)|浏览(114)

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

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

conf

  1. <IfModule mod_ssl.c>
  2. <VirtualHost *:443>
  3. DocumentRoot "/home/...path.../public"
  4. ServerName example.com
  5. Alias /lib /home/...path.../otherlib/
  6. <Directory />
  7. Options Indexes FollowSymLinks
  8. AllowOverride All
  9. Require all granted
  10. </Directory>
  11. RewriteEngine on
  12. RewriteCond %{REQUEST_URI} !^/lib/ [NC]
  13. RewriteCond %{REQUEST_FILENAME} !-f
  14. RewriteCond %{REQUEST_FILENAME} !-d
  15. RewriteRule ^(.*)$ /index.php [NC,L,QSA]
  16. </VirtualHost>
  17. </IfModule>
b1payxdu

b1payxdu1#

  1. RewriteEngine on
  2. RewriteCond %{REQUEST_URI} !^/lib/ [NC]
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteRule ^(.*)$ /index.php [NC,L,QSA]

当在 virtualhost 上下文中使用时,mod_rewrite的处理要早得多,在请求Map到文件系统之前。因此,REQUEST_FILENAME还不包含请求的URLMap到的完整文件系统路径,它只包含根相对URL(与REQUEST_URI相同)。所以,这两个“否定”的条件总是成功的。(所以你所有的静态资产也将被重写到前端控制器中。)
你需要要么,使用前瞻,例如。%{LA-U:REQUEST_FILENAME}或从DOCUMENT_ROOTREQUEST_URI服务器变量构造文件名,这在任何一种上下文中都有效。举例来说:

  1. RewriteCond %{REQUEST_URI} !^/lib/ [NC]
  2. RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
  3. RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
  4. RewriteRule ^/. /index.php [L]

我还简化了RewriteRule模式,因为这里不需要捕获URL路径,也不需要匹配根目录。NCQSA标志也是不必要的。
我可以在.htaccess中做的是将example.com/s1/s2/s3作为php中$_REQUEST数组中的3个元素。
但是,上面没有做到这一点。使用上述指令,您可以从PHP中的单个$_SERVER['REQUEST_URI']超全局元素访问整个URL,然后需要解析/分解以提取s1s2s3
如果你特别想要$_REQUEST数组中的3个元素,那么你需要将RewriteRule指令改为如下所示:

  1. :
  2. RewriteRule ^/([^/]+)/([^/]+)/([^/]+)$ /index.php?s1=$1&s2=$2&s3=$3 [L]

但这只匹配/foo/bar/baz的URL。像/foo/bar这样的双路径段URL将不匹配。

展开查看全部

相关问题