Articles of mod rewrite

使用mod_rewrite从URL中删除PATH_INFO

更新:大部分解决。 现在只有在您尝试访问http://www.example.com/any-real-file-without-an-extension/anything-afterwards-that-doesnt-end-in-a-slash的情况下http://www.example.com/any-real-file-without-an-extension/anything-afterwards-that-doesnt-end-in-a-slash 。 我猜服务器认为any-real-file-without-an-extension可能是一个目录…不知道如何解决这个问题。 PHP将“PATH_INFO”定义为URL的一部分: 包含任何客户端提供的path名信息,尾随实际的脚本文件名,但在查询string之前(如果可用)。 例如,如果当前脚本通过URL http://www.example.com/php/path_info.php/some/stuff?foo=bar ,那么$_SERVER['PATH_INFO']将包含/some/stuff 。 截至目前,我的网站将http://example.com/ contact_us.php/ANYTEXTCANGOHERE视为http://example.com/contact_us.php 在这种情况下, /ANYTEXTCANGOHERE是PATH_INFO。 我想这样做,这个path信息被删除到文件名,否则返回一个404。 有什么办法可以做到吗? 这里是我的.htaccess现在看起来像。 这很简单。 只是一个301redirect到www和最后的反斜杠,以及强制关联到文件没有扩展名和重写没有扩展。 我没有服务器root权限。 ErrorDocument 404 /404.php ErrorDocument 503 /503.php #Force www: RewriteEngine on RewriteCond %{HTTP_HOST} ^foodthing.org [NC] RewriteRule ^(.*)$ http://www.foodthing.org/$1 [L,R=301,NC] #get rid of trailing slashes RewriteCond %{HTTP_HOST} ^(www.)?foodthing\.org$ [NC] RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] #1)externally redirect "/file.php" to […]

Apache的情况下,单个规则不敏感的mod_rewrite

我正在使用mod_rewritesearch引擎友好的url。 我有这个很好的作品: RewriteRule ^Pay ./pay.php [L] #Pay 但是如果访问者inputhttp://example.com/pay (注意小写),我也想要匹配。 我曾尝试使用NC但我得到一个500错误。 我已经尝试了2个单独的规则一个大写和一个小写,但再次500错误。 这可以使用正则expression式在一行上完成吗? 就像是: RewriteRule ^([Pp])ay ./pay.php [L] #Pay 如果是这样,什么是正确的方法? 如果不是我怎么能不使用mod_spelling来完成这个?

重写规则失去了查询参数

我有一个运行apache2和php7的networking服务器。 在我的apacheconfiguration中,有如下的redirect规则: RewriteCond %{HTTP_HOST} !^www.* RewriteRule .* %{HTTP:X-Forwarded-Proto}://www.%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L] 这应该执行http(s)://example.com/scpript?param1=x&param2=yredirect到http(s)://www.example.com/scpript?param1=x&param2=y 它基本上工作,只有一个例外:只有第一个getparameter passing给rewitten url,所以我的客户端实际上收到http(s)://www.example.com/scpript?param1=x作为redirect目标。 我很困惑,我怎么让系统通过完整的查询呢? 如果还试过 RewriteRule .* %{HTTP:X-Forwarded-Proto}://www.%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [QSA,R=301,L] 但是,这会将我redirect到http(s)://www.example.com/scpript?param1=x&param1=x 。 在重写开始之前,它看起来像是省略了其他参数。

用于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 […]

Apache2 – mod_rewrite:RequestHeader和环境variables

我尝试获取请求参数“授权”的值,并将其存储在请求的标题“授权”中。 第一个重写规则工作正常。 在第二个重写规则中,$ 2的值似乎并不存储在环境variables中。 结果请求头“授权”是空的。 任何想法 ? 谢谢。 <VirtualHost *:8010> RewriteLog "/var/apache2/logs/rewrite.log" RewriteLogLevel 9 RewriteEngine On RewriteRule ^/(.*)&authorization=@(.*)@(.*) http://<ip>:<port>/$1&authorization=@$2@$3 [L,P] RewriteRule ^/(.*)&authorization=@(.*)@(.*) – [E=AUTHORIZATION:$2,NE] RequestHeader add "Authorization" "%{AUTHORIZATION}e" </VirtualHost> 我需要处理几个案例,因为有时参数在path中,而他们在查询中。 取决于用户。 这最后一个案件失败。 AUTHORIZATION的标题值看起来是空的。 # if the query string includes the authorization parameter RewriteCond %{QUERY_STRING} ^(.*)authorization=@(.*)@(.*)$ # keep the value of the parameter in the AUTHORIZATION […]

阻止MIME的盗链请求

我试图阻止人们盗链到PDF和DOC文件。 通常,我会用这样的.htaccess规则来处理这个问题: RewriteEngine On RewriteBase / RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC] RewriteRule \.(pdf|doc)$ /home/ [R=302,L] 然而,这些文件中的许多文件通过php脚本链接,例如filedownload.php?id = 5,然后触发PDF / DOC文件的下载。 有没有办法防止通过输出文件的MIME链接到这些文件? 其他方式? 编辑 – 添加此源来显示如何调用文件: header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: $ctype"); header("Content-Disposition: attachment; filename=\"".basename($fn)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$fs); echo $upload["file_contents"]; exit();

htaccess mod重写问题

让我立即开始我的问题: Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1 [L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2 [L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3 [L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L] RewriteRule ^([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1 [R=301,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2 [R=301,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3 [R=301,L] RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3/$4 [R=301,L] 有文件夹sinj.com.hr/administracija ,当我尝试访问http://localhost/sinj.com.hr/administracija我被redirect到 HTTP://localhost/sinj.com.hr/administracija VAR1 = administracija 我想要的是,当用户inputhttp://localhost/sinj.com.hr/administracija ,他被redirect到http://localhost/sinj.com.hr/administracija/index.php 。 我试图用头(“位置:…”)做到这一点,但它总是将我redirect到http://localhost/sinj.com.hr/administracija?var1=administracija 。 如果文件夹administracija重命名然后header()函数工作。 任何想法如何解决这个问题? 谢谢,Ile

nginx重写规则

我有以下mod_rewrite规则 RewriteCond %{HTTP_HOST} !^(host)\.doamin\.com [NC] RewriteCond %{HTTP_HOST} !^(www)\.domain\.com [NC] RewriteRule ^(.*) /magento/$1 [L] 我需要在nginx上工作,而且我一直在靠墙敲门,以使其工作 谢谢! UPDATE! 这是对我正在尝试做的更好的解释 store1.domain.com store2.domain.com 所以用户可以进入store1.domain.com/products/url将保持在那里 我们在apache中这样做 RewriteCond %{HTTP_HOST} !^(host)\.domain\.com [NC] RewriteCond %{HTTP_HOST} !^(www)\.domain\.com [NC] RewriteRule ^(.*) /magento/$1 [L] 我们也有一个dns catchall,将所有* .domain.com发送到默认的apache虚拟主机。 我正在靠墙敲着头,让它在nginx中工作,以保持主机名相同,但在幕后重写magentoparsing域。 这是我有,但它只是继续追加/ magento / magento / magento到最后,直到它杀死循环 if ($http_host !~ "^www.domain\.com$") { rewrite ^.+ http://$http_host/magento/$uri last; break; } 上面的问题是不断重写它 […]

在某些页面直接http到https?

下面是我添加到我的.htaccess代码的一些代码,我如何添加某些页面被redirect到https? (如login.php&login.html) 另外,如果用户inputwww。 他们得到一个“不可信的连接”,因为SSL只有在没有www的情况下才有效。 我怎么能解决这个问题? RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /login.html RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

第一个条件之后Apache mod_rewrite停止

我试图把这个URL改写如下: fr.example.com/tag/movies example.com/catch.php?tag=movies fr.example.com » domain.com/catch.php?country=fr 对于这些,我有一个.htaccess文件中的以下mod_rewrite规则 子域名+标签 RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com [NC] RewriteRule ^tag/([^/]+) catch.php?country=%1&tag=$1 [L,NS] 标签 RewriteRule ^tag/([^/]+) catch.php?tag=$1 [L,NS] 子域 RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com [NC] RewriteRule ^(.*)$ catch.php?country=%1 [L,NS] 按照这个顺序。 现在,第一个RewriteRule之后的L应该结束处理,并将其redirect到子域名+标签URL,但是不会。 例如, fr.example.com/tag/movies发送到catch.php?country=fr ,这是.htaccess文件中的最后一个条件。 发现第一个L后不应该停止吗? 回答 那么,事实certificate, [L]标志根据上下文( .htaccess / <Directory>不同于httpd.conf )的不同而有所不同,正如您在这里所看到的。 因此,为了使其工作,只需将规则放置在<VirtualHost> (NOT <Directory> )中,并将/添加到每个URI( /catch.php /tag ,…)。 而已。