Articles of mod rewrite

Mod_Rewrite Apache ProxyPass?

我有两个网站 OLDSITE和新闻网站。 OLDSITE拥有120个IP地址,新闻网站有5个。我希望能够将所有事情从OLDSITE和NEWSITE中分离出来,这样他们就不会被绑在一起,而是可以在同一台Linux计算机上使用它们。 我目前的Apache设置是这样的: Listen 80 NameVirtualHost * <VirtualHost *> ServerName oldsite.com ServerAdmin [email protected] DocumentRoot /var/www/ <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> RewriteEngine on RewriteCond %{HTTP_HOST} ^([^.]+)\.oldsite\.com$ RewriteCond /home/%1/ -d RewriteRule ^(.+) %{HTTP_HOST}$1 RewriteRule ^([^.]+)\.oldsite\.com/media/(.*) /home/$1/dir/media/$2 RewriteRule ^([^.]+)\.oldsite\.com/(.*) /home/$1/www/$2 </VirtualHost> <VirtualHost newsite.com> ServerName newsite.com ServerAdmin [email protected] DocumentRoot […]

如何使重写规则相对于.htaccess文件

目前我有一个这样的.htaccess文件。 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f [OR] RewriteCond %{REQUEST_URI} ^/(always|rewrite|these|dirs)/ [NC] RewriteRule ^(.*)$ router.php [L,QSA] 当网站文件位于networking服务器的document_root(即domain.com/abc.php – > <DOCUMENT_ROOT>/abc.php )时,它会生成。 但在我们目前的设置(这是不可改变的),这不能保证。 我们有时可以在.htaccess文件的文档根目录和文件夹之间有任意文件夹(即domain.com/something/abc.php – > <DOCUMENT_ROOT>/something/abc.php )。 唯一的问题是,第二个RewriteCond不再有效。 有没有办法dynamic检查path相对于.htaccess文件访问的path。 例如: 如果我有一个站点,其中domain.com/prefix/是.htaccess文件的目录。 NOT FORCED TO REWRITE -> domain.com/prefix/index.php FORCED TO REWRITE -> domain.com/prefix/rewrite/index.php 如果我有一个网站,其中domain.com/是.htaccess文件的目录。 NOT FORCED TO REWRITE -> domain.com/index.php FORCED TO REWRITE -> domain.com/rewrite/index.php

Apache重写规则不匹配冒号(:)

我在编写规则时遇到了麻烦,它会将http:// localhost / hello:world这样的地址重写为http://localhost/hello/world.html 我在httpd.conf中的RewriteRule如下所示: <Directory "D:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all RewriteEngine On RewriteBase / RewriteRule ^hello:world$ /hello/world.html [L,QSA] #Doesn't work RewriteRule ^hello_world$ /hello/world.html [L,QSA] #Works great! </Directory> 当我尝试http:// localhost / hello:world时 ,我收到一个403 Forbidden页面。 特别要注意的是,这个规则 RewriteRule ^hello_world$ /hello/world.html [L,QSA] 通过http:// localhost / hello_world工作得很好。 我在Windows Server […]

Apache mod_rewrite – 删除扩展/添加尾部斜杠/保留目录结构

我正在使用下面的.htaccess文件从我的文件中删除.php扩展名,并为所有URL添加一个尾部的斜杠: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([^/]+)/$ $1.php # Forces a trailing slash to be added RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L] 这是工作很好,但是,当我有一个目录中的PHP文件,我想从一个目录服务该文件,我得到一个404错误。 有没有办法做到这一点在根.htaccess文件。 我真的不想记得在每个目录中放一个.htaccess文件。 马上 www.myexample.com/information/ 提供/information.php 。 大! 然而 www.myexample.com/categories/category-1/ 即使文件/categories/category-1.php存在,也会抛出404错误。 我想修改我的.htaccess文件,这样服务/categories/category-1.php 。

将nginx转换为Apache重写

我试图从nginx上的现有网站设置CodeIgniter。 我的本地机器是Apache,这是nginx上的redirectscheme。 我怎么写使用RewriteRules ? if ($host ~* ^www\.(.*)) { set $host_without_www $1; rewrite ^(.*)$ http://$host_without_www$1 permanent; } if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$){ rewrite ^(.*)$ / permanent; } if ($request_uri ~* index/?$){ rewrite ^/(.*)/index/?$ /$1 permanent; } if (!-d $request_filename){ rewrite ^/(.+)/$ /$1 permanent; } if ($request_uri ~* ^/system){ rewrite ^/(.*)$ /index.php?/$1 last; break; } if (!-e […]

使用AddEncoding x-gzip .gz没有实际的文件

使用Apache(2.2及更高版本),我如何实现以下function。 当使用GZip编码( 不是简单的Deflate)输出某个文件时,我想要使用它的名字加上扩展名.gz ,其中的.gz版本在磁盘上并不存在。 假设我有一个名为/path/foo.bar的文件,并且在URI /path映射到的文件夹中没有文件foo.bar.gz ,我怎样才能让Apache服务于/path/foo.bar的内容,但是使用AddEncoding x-gzip …应用于(不存在的)文件? 重写部分似乎很容易,但问题是如何将编码应用到不存在的项目。 另一种方式似乎也很简单,只要客户端支持编码。 唯一的解决scheme真的是一个脚本,在飞行中做到这一点? 我知道mod_deflate和mod_gzip ,这不是我正在寻找 – 至less不是一个人。 特别是我需要一个实际的GZIP文件,而不仅仅是一个放气的stream。 现在我正在考虑使用mod_ext_filter,但是无法弥合将(不存在的) file.gz的名称重写到一边的file和另一边的LocationMatch之间的差距。 这是我的。 RewriteRule ^(.*?\.ext)\.gz$ $1 [L] ExtFilterDefine gzip mode=output cmd="/bin/gzip" <LocationMatch "/my-files/special-path/.*?\.ext\.gz"> AddType application/octet-stream .ext.gz SetOutputFilter gzip Header set Content-Encoding gzip </LocationMatch> 请注意,在这种情况下,客户端确实不需要Content-Encoding的头文件。 他们希望看到实际的GZIP文件,但是我想在没有caching的情况下执行此操作(这是一个testing场景)。

检查文件是否存在的RewriteCond会导致IO延迟?

我正在设置一个Apacheconfiguration来检查文件的存在。 如果文件存在,则服务器将redirect到维护页面。 否则,应用程序将正常提供服务。 我担心,有大量的请求,文件检查必须发生在每个单独的请求,并可能最终导致大量的不必要的IO。 检查文件是否存在IO开销? 如果是这样, 是否有任何替代检查文件的存在? 我需要能够修改Apache以外的东西,不需要重新启动或优雅。 例如,是否有一个模块允许查询一个memcached实例是否存在密钥? 还是有任何其他types的内存caching可以使用?

通过.htaccessredirect到shebang的path?

我有一小部分代码可以将从我自己的域访问的页面redirect到他们的哈希地址,如下所示: example.com/stationary.html => example.com/#/stationary 这是代码: <IfModule mod_rewrite.c> # Redirect requests to ajax pages the their hashes RewriteCond $1 !=index [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC] RewriteRule ^([a-zA-Z0-9\-_]+)\.html?$ /#$1 [NE,R] </IfModule> 这将工作很好,现在我想知道我可以得到它redirect页面 example.com/stationary => example.com/#!/stationary 黑客上面的代码没有工作。 任何帮助,将不胜感激! 谢谢!

阻止所有用户无引荐者,但允许Googlebot / bingbot在同一时间(使用.htaccess)

由于一些业余的DDOS攻击我的网站,我不得不否认一些交通.htaccess工作正常。 不幸的是,它也阻止了googlebot / bingbot: order allow, deny deny from 54. SetEnvIfNoCase Referer "^$" bad_user SetEnvIfNoCase User-Agent "^Wget" bad_user Deny from env=bad_user 它只是阻止来自54.xxx整个stream量(只有来自它的stream量来自受感染的亚马逊云 – 我知道我可以排除亚马逊云只有30个IP范围,而不是整个54.xxx但我需要快速解决scheme)。 其余的机器人(大多数来自中国,台湾等)不使用引用,所以: SetEnvIfNoCase Referer "^$" bad_user 阻止他们。 但它也有一个副作用 : 当有人通过书签访问我的页面,或者直接input到浏览器(例如他在名片上显示红色)时,他将不会看到我的网站。 Googlebot,bingbot(以及其他不太重要的机器人)通常也不使用引荐来源。 #1是一个不便,但#2是一个真正的问题,我必须迅速解决。 我发现对我来说重要的机器人使用这些标签: 66.249.64.119 – – […] "GET /robots.txt HTTP/1.1" 403 534 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" 66.249.64.119 – – […] "GET […]

CentOS 6.8,Apache 2.2,稳定多年,突然间mod_rewrite似乎失败了

我有一个稳定多年的设置。 突然间,我得到的文件不应该存在于应该由mod_rewrite处理的项目上的Apache错误日志。 昨天晚上,我做了一个PHP / MySQL(不是Apache)的包更新,但我回滚了,它仍然有问题。 .htaccess和apacheconfiguration没有被触及。 我仍然在寻找线索,但到目前为止我几乎没有任何线索。 看起来像我们从本地networking(这是一个不同的服务器托pipe的networking)内的任何东西都没有问题。 如果我从我的手机打开了无线,我有时看到问题。 所有的文件不存在的path是有效的mod_rewrite处理。 我可以打他们没有问题。 有点困惑,并在接下来要寻找什么。 我inheritance了这个configuration,我试过不去碰它,但是这里是我们为这个虚拟主机准备的。 RewriteEngine on RewriteCond %{HTTP_HOST} !^www.domain.com RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L] 在公共文件夹中也有一个.htaccess与很多configuration。 这是一个相关的块: RewriteEngine On RewriteBase / #Correct some bad external links RewriteRule ^lh$ / [L,R=301] <FilesMatch "^cart$"> ForceType application/x-httpd-php </FilesMatch> 我已经把mod_rewritelogging到了9,并且匹配了apache错误日志中的一个请求。 init rewrite engine with requested uri /cart/ViewItem/6054668 applying pattern '^(.*)$' to uri […]