用于ScriptAlias位置上查询string的mod_rewrite规则

安装:在Ubuntu的Apache-2.4.18上通过FastCGI的一个QGis-2.18服务器 (真的是一个embedded的MapServer )实例。

如果在cgi处理程序的查询string中设置了某个值,我想添加另一个值。 为此,我在/etc/apache2/conf-enabled/qgis.conf的顶部添加了三行:

 RewriteEngine on RewriteCond "%{QUERY_STRING}" "application/json" [NC] RewriteRule "^/$" "/?OUTPUTFORMAT=GeoJSON" [PT,QSA] ScriptAlias / /usr/lib/cgi-bin/qgis_mapserv.fcgi <Location "/"> SetHandler fcgid-script Require all granted PassEnv QGIS_PROJECT_FILE </Location> FcgidInitialEnv QGIS_LOG_FILE ${QGIS_LOG_FILE} FcgidInitialEnv QGIS_SERVER_LOG_FILE ${QGIS_SERVER_LOG_FILE} FcgidInitialEnv QGIS_DEBUG ${QGIS_DEBUG} FcgidInitialEnv QGIS_SERVER_LOG_LEVEL ${QGIS_SERVER_LOG_LEVEL} FcgidInitialEnv QGIS_PLUGINPATH "${QGIS_PLUGINPATH}" FcgidInitialEnv PGSERVICEFILE ${PGSERVICEFILE} FcgidInitialEnv HOME /var/www 

我正在像这样访问服务器:

 http://myserver.invalid.tld:51081/ ?SERVICE=WFS &VERSION=1.1.0 &REQUEST=GetFeature &OUTPUTFORMAT=application/json &MAXFEATURES=1 &SRSNAME=EPSG:4326 &TYPENAME=feature_type_name &BBOX=8.5985658,56.447691,8.600106,56.448553 

我会期望效果是一样的,如果我手动添加&OUTPUTFORMAT=GeoJSON在我的URL结束,但我看到重新启动Apache后没有任何区别 。 (是的,我已经运行sudo a2enmod rewrite 。)

我不确定重写规则和脚本别名之间的相互作用是如何工作的,所以我在考虑一个是否会影响另一个呢? 不幸的是,我也不知道如何debugging。

服务器有一个虚拟主机启用,看起来像OOTBconfiguration给我:

 # apache2ctl -S AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.10. Set the 'ServerName' directive globally to suppress this message VirtualHost configuration: *:80 172.17.0.10 (/etc/apache2/sites-enabled/000-default.conf:1) ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/proc/self/fd/2" Mutex default: dir="/var/lock/apache2" mechanism=fcntl Mutex fcgid-pipe: using_defaults Mutex watchdog-callback: using_defaults Mutex rewrite-map: using_defaults Mutex fcgid-proctbl: using_defaults PidFile: "/var/run/apache2/apache2.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="www-data" id=33 Group: name="www-data" id=33 

这里是/etc/apache2/sites-enabled/000-default.conf (剥离注释):

 <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog /proc/self/fd/2 CustomLog /proc/self/fd/1 combined </VirtualHost> 

最后apache2.conf (再次,评论剥离):

 Mutex file:${APACHE_LOCK_DIR} default PidFile ${APACHE_PID_FILE} Timeout 300 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5 User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} HostnameLookups Off ErrorLog /proc/self/fd/2 LogLevel warn IncludeOptional mods-enabled/*.load IncludeOptional mods-enabled/*.conf Include ports.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 None Require all granted </Directory> AccessFileName .htaccess <FilesMatch "^\.ht"> Require all denied </FilesMatch> LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %O" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent IncludeOptional conf-enabled/*.conf IncludeOptional sites-enabled/*.conf 

(对于奖励点,我想用GeoJSONreplace查询string中的application/json ,但是如果我能得到参数后面跟着我接近目标)。

我会期望效果是一样的,如果我手动添加&OUTPUTFORMAT=GeoJSON

那也许是“问题”呢? 它不附加 。 请求中的原始查询string被追加 。 查询string中的查询string( OUTPUTFORMAT=GeoJSON )位于查询string的开头。 所以,根据你如何阅读/parsing查询string中的参数,你的新设置可能被覆盖。

要特别附加一些东西到现有的查询string中,您可以使用替代中的QUERY_STRING服务器variables(而不是使用QSA标志)。 例如:

 RewriteCond %{QUERY_STRING} application/json RewriteRule ^/$ /?%{QUERY_STRING}&OUTPUTFORMAT=GeoJSON [PT] 

除非包含空格,否则不需要将每个参数都包含在引号中。 如果您特别需要不区分大小写的匹配,请仅使用NC标志。

或者,要replace请求中的OUTPUTFORMAT=application/json URL参数,则可以这样做:

 RewriteCond %{QUERY_STRING} (.*)OUTPUTFORMAT=application/json(.*) RewriteRule ^/$ /?%1OUTPUTFORMAT=GeoJSON%2 [PT] 

%1%2是前面的CondPattern中捕获的组的反向引用。 即。 查询string中原始URL参数前后的所有内容。