Articles of mod rewrite

为什么RewriteBase不起作用?

这是我正在做的事情: 域名是thinkingmonkey.me 域名具有127.0.0.1作为IP地址 mod_alias已安装。 我有一个名为directories.conf的conf文件。 其中我有所有有关目录的configuration。 directories.conf包含在httpd.conf 我的directories.conf有 Alias /runs /xhprof/xhprof_html <Directory /mysite/xhprof/xhprof_html> Order allow,deny Allow from all AllowOverride All </Directory> 在/mysite/xhprof/xhprof_html/.htaccess 。 我有以下几点: RewriteEngine on RewriteBase /runs RewriteRule .* index.php 我所要做的就是将/mysite/xhprof/xhprof_html/下的任何请求指向index.php 。 当我请求不带结尾斜线的 thinkingmonkey.me/runs ,我404 not found 。 所以,我推断RewriteBase不起作用。 我究竟做错了什么?

Apache的mod_rewrite启用,但不能正常工作?

在VPS上运行Ubuntu Server 10.10。 运行mod_rewrite启用Apache2。 phpinfo()显示mod_rewrite。 core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5 mod_proxy mod_proxy_http mod_reqtimeout mod_rewrite mod_setenvif mod_status Apache刚刚安装,没有真正的变化..刚刚得到这个VPS开始试验。 我使用这个命令启用mod_rewrite: sudo a2enmod rewrite 我创build了我的.htaccess文件,并将其添加到包含以下代码的www目录中: RewriteEngine on RewriteRule ^alice.html$ bob.html 这两个html文件都与.htaccess文件位于相同的www目录中,我确保在更改后重新启动服务器,并确保没有简单的拼写或大小写错误。 任何build议将不胜感激,谢谢。

在mod_rewrite之前应用mod_cband?

我目前在Apache中有一个使用mod_cband和mod_rewrite的虚拟主机。 我正在使用mod_cband来限制每个远程用户的连接数量。 超过此限制时,应该返回503 Service Unavailable ,这是mod_cband默认值。 如果没有超出限制,那么mod_rewrite应该根据我指定的规则重写URL。 不幸的是, mod_rewrite似乎比mod_cband有一些优先级,因为即使在超出限制的情况下也会发生重写,并且503 Service Unavailable从不返回。 反正mod_rewrite之前强制mod_cband应用吗? configuration如下: <VirtualHost *:80> ServerName a.domain.com ServerAdmin [email protected] CBandRemoteSpeed 1024 1 10 RewriteEngine on # Some other rewrite rules. RewriteRule (.*) https://a.domain.com? [R] … </VirtualHost>

apache短路mod重写条件

这是我的问题 我的简单的正则expression式工作得很好,我只是想确保正则expression式的效率第一部分短路,我把我的问题在线 1-这个东西应该做的是,如果文件存在,那么带有列表扩展名的文件不应该被重写,如果文件不存在,我们重写请求到我们的PHP文件,所有其他文件types总是重写,不pipe文件是否存在。 第一个条件,该文件是否有以下任何扩展名? 例如: RewriteCond %{REQUEST_FILENAME} \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$ 第二种情况,如果文件是我们的扩展之一,它是否存在于文件系统上? 对这个声明的评估是昂贵的,我想确保我们在以前的RewriteCond上短路,并且只有在上述评估为真的情况下才会检查这个值。 还需要知道如果操作系统或Apache将caching文件的存在的结果(例如PHP的cachingis_file()的结果) 例如: RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ my_index.php?q=$1 [L,QSA] 目录和文件独自一人….因为如果他们通过 文件存在testing,他们不应该被重写 例如: RewriteRule \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$ – [L] RewriteRule ^robots.txt$ – [L] RewriteRule ^(.*)$ my_index.php?q=$1 [L,QSA]

在htaccess重写规则中使用SetEnvvariables

在我们的httpd.conf虚拟主机块中 SetEnv MAGE_RUN_CODE "v2_us_es" 是否有可能在.htaccess文件中使用该variables 就像是 RewriteRule ^sitemap\.xml$ /media/sitemaps/%{MAGE_RUN_CODE}/sitemap.xml [L] 这可能吗? 谢谢! 杰夫 编辑:我查看了以下链接,但他们没有做我想做的事情 在 https://stackoverflow.com/questions/2008123/how-to-use-getenv-in-php-and-setenv-in-a -htaccess-a-compiled-php-cgi-on https://stackoverflow.com/questions/3638637/how-to-use-the-setenv-variable-in-apache

为什么我的重写规则导致“400:错误的请求”?

我的网站configuration中有这些重写规则: RewriteEngine on RewriteRule ^app.php – [L] RewriteRule ^(.*)$ app.php$0 [L] 目标是所有的请求应该被附加到前端控制器“app.php”。 规则的最后一行导致我的服务器以“400:错误的请求”响应,无论我尝试什么path,甚至是空path。 我已经启用了重写日志,并尝试了两个path:“/”和“/ login”。 这是日志说: (2)init请求的uri / (3)将模式'^ app.php'应用于uri'/' (3)将模式'^(。*)$'应用于uri'/' (2)重写'/' – >'app.php /' (2)本地path结果:app.php / (2)用请求的uri / login初始化重写引擎 (3)将模式'^ app.php'应用于uri'/ login' (3)将模式'^(。*)$'应用于uri'/ login' (2)重写'/ login' – >'app.php / login' (2)本地path结果:app.php / login 根据日志,它应该成功。 如果我删除重写规则的最后一行,并访问URL“app.php / login”,那么它按预期工作。 有没有人知道为什么这不起作用? 注意:错误日志中没有错误。 注意:当最后的重写规则被删除时,结果path(“app.php /”,“app.php / login”)完美地工作。

将Apache 500状态转发到404

简短的版本 :我们希望Apache在获得500状态时捕获并返回类似404的东西。 有没有办法做到这一点? 更长的版本 :这些服务器运行的应用程序不是我们的,而且最近他们没有通过PCI符合性扫描,因为如果您弄糟了主机,可能会出现500个状态错误。 从长远来看,我们将把它们移动到不同的networking,因此它们不需要符合PCI规范,但是从短期来看,我们希望能够快速修复,而不是显示应用程序返回的500状态。 这两个应用程序都通过Apache接受他们的请求。 我希望/假设这将是一个mod_rewrite规则,但不知道根据状态码使mod_rewrite更改。

使用子path内部代理的子域名,但redirect外部客户端,如果他们要求该子path?

我有一个VirtualHost,我想有几个子域名。 (为了清楚起见,假设我的域名是example.com ,我只是想通过让foo.example.com工作,并从那里开始构build。) 我发现一个子域与我的框架非侵入性地工作最简单的方法是通过mod_rewrite代理到一个子path。 因此,path将在客户端的URL栏中显示为http://foo.example.com/(无论什么),而他们实际上将被提供http://foo.example.com/foo/(whatever) 。 我设法在我的VirtualHostconfiguration文件中这样做: ServerAlias *.example.com RewriteEngine on RewriteCond %{HTTP_HOST} ^foo\.example\.com [NC] # <— RewriteCond %{REQUEST_URI} !^/foo/.*$ [NC] # AND is implicit with above RewriteRule ^/(.*)$ /foo/$1 [PT] (注意:很难find这个特定的工作组合,特别是在[RewriteRule]中,[PT]似乎是必须的,我无法用我在别处看到的例子[L] 。它不会显示任何东西或进入循环。也有一些浏览器似乎caching响应页面的糟糕的循环,一旦他们得到一个页面重新加载后修复它不会显示它正在工作!任何情况下 – 如果这部分可以做得更好。) 现在我想让什么http://foo.example.com/foo/(无论)提供取决于谁问。 如果请求来自外部,我希望客户端被Apache永久redirect,以便他们在浏览器中获得URL http://foo.example.com/(无论什么) 。 如果它来自mod_rewrite的内部,我想要的是由Web框架处理…这是不知道子域。 是这样的可能吗?

当通过301从HTTPredirect到Apache中的HTTPS来规范化域时,是否所有域都必须位于SSL证书上?

我有几个域指向同一台服务器。 只有其中一个域名(主要域名)实际上位于我的SSL证书上。 我在httpd.conf文件中包含了以下内容,所以它们都会永久redirect到一个域。 RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com$1 [R=301,L] 意图是所有域名必须redirect到www.example.com作为HTTPS。 不过,我最近注意到这些redirect正在发生 – 但作为一个302redirect。 我在这里尝试了一个理智的检查,似乎按照我预期的那样工作。 所以回顾一下: 会发生什么情况: http : //sub1.example.com会导致302redirect到https://www.example.com 。 应该发生什么: http : //sub1.example.com会导致301redirect到https://www.example.com 。 我的问题是,所有的域名都必须在SSL证书上列出才能使301redirect发生? 我只是在这里拖着稻草。 只是为了完整性,这是发生这种情况时,初始页面和目标页面的请求/响应标头。 初始页面(sub1.example.com)的请求标题: GET / HTTP/1.1 Host: sub1.example.com Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 Upgrade-Insecure-Requests: 1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 […]

VirtualHosts上的Apache RewriteRule不适用于组捕获正则expression式

我需要创build一个RewriteRule来将像/tdg/image.jpg?mode=crop&width=300&height=300这样的URLpath/tdg/image.jpg?mode=crop&width=300&height=300给本地代理。 代理需要将给定的URL转换为以下格式。 http://localhost:8888/unsafe/300×300/smart/tdg/image.jpg 我先尝试使用ProxyPassMatch apache指令,但是我无法从查询string中检索宽度和高度数据。 ProxyRequests On ProxyPreserveHost On ProxyPassMatch ^\/(tdg.+\.(?:png|jpeg|jpg|gif))\?mode=crop.+width=(d+)&height=(d+) http://localhost:8888/unsafe/$2x$3/smart/$1 我也试过RewriteRule RewriteEngine On RewriteRule ^\/(tdg.+\.(?:png|jpeg|jpg|gif))\?mode=crop.+width=(d+)&height=(d+) http://localhost:8888/unsafe/$2x$3/smart/$1 在这两种情况下,代理的结果URL都是http://localhost:8888/unsafe/x/smart/$1 ,其中应该是http://localhost:8888/unsafe/300×300/smart/tdg/image.jpg 我不知道为什么我不能使用group正则expression式从查询string中获取width和height值。