我正在通过 nginx 提供一个静态站点,我的目标是替换如下所示的 URL:
和
关键是没有尾部斜杠。我目前正在使用位置别名做类似的事情,但这很乏味,因为它需要每个文件的位置块,并且它还附加一个斜杠,因为 nginx 将别名视为目录:
location / {
root /srv/www/foo/public_html;
index index.html;
}
location /bar1 {
alias /srv/www/foo/public_html/;
index bar1.html;
}
location /bar2 {
alias /srv/www/foo/public_html/;
index bar2.html;
}
Run Code Online (Sandbox Code Playgroud)
等等。我已经通读了有关重写的文档,但似乎无法将所说的内容与我需要做的事情综合起来。我不是来自 Apache 背景;nginx 是我第一次涉足网络服务器,所以我确定我错过了一些明显的东西,因为我的 HTTP 背景很弱。在此先感谢您提供的任何帮助。
我一直在尝试在启用了静态 HTML 页面和压缩的站点上的 Windows 2008 R2 上的 IIS 7.5 中创建出站规则。
如果我启用了任何形式的压缩(静态或动态),我会收到此错误:
HTTP Error 500.52 - URL Rewrite Module Error. Outbound rewrite rules cannot
be applied when the content of the HTTP response is encoded ("gzip").
Run Code Online (Sandbox Code Playgroud)
由于会对页面加载性能产生巨大的负面影响,因此关闭压缩并不是一个真正的选择。
我想任何使用出站重写规则的人都会遇到这个错误,因为您总是希望为文本内容启用压缩。
我查看了一些建议以使其正常工作:
和
http://forums.iis.net/p/1165899/1937454.aspx#1937454
但我仍然得到同样的错误。
有没有人让这个工作?如果是这样怎么办?
我有一个 CMS 中干净 URL 的 URL 重写设置,我的 web.config 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Clean URLs" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="?id={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
它基本上变成index.php?id=something为something清洁的网址。非常简单,而且效果很好。
在 CMS 中很常见,为了防止后端中断,每个子目录都需要<remove name="Clean URLs" />或<clear />在其 web.config 中,因此规则不会被继承。
有没有办法在父规则中指定它根本不应该被子规则继承,方法是将规则的范围限制在当前目录?类似的东西<rule name="Clean URLs" stopProcessing="true" inherit="no">将是史诗般的。
是否可以替换通过代理传递的每个页面上的内容,类似于 mod_rewrite 用于 URL 的方式?关于替代品的文件不清楚。
我有一些我正在反向代理的页面具有绝对路径。这破坏了网站。它们需要替换,并且 mod_rewrite 之类的工具没有选择它们,因为它们不是 URL 请求。
<VirtualHost *:80>
ServerName servername1
ServerAlias servername2
ErrorLog "/var/log/proxy/jpuat_prox_error_log"
CustomLog "/var/log/proxy/jpuat_prox_access_log" common
RewriteEngine on
LogLevel alert rewrite:trace2
RewriteCond %{HTTP_HOST} /uat.site.co.jp$ [NC]
RewriteRule ^(.*)$ http://jp.uat.site2uk.co.uk/$1 [P]
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|uat.site.co.jp|jp.uat.site2uk.co.uk|i"
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://uat.site.co.jp/
ProxyPassReverse / http://uat.site.co.jp/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
以上都不适用于替换 HTML 字符串
<link href="//uat.site.co.jp/css/css.css
Run Code Online (Sandbox Code Playgroud)
和
<link href="//uat.site2uk.co.uk/css/css.css
Run Code Online (Sandbox Code Playgroud)
<VirtualHost *:80>
ServerName jp.uat.site2uk.co.uk
ServerAlias uat.site.co.jp
ErrorLog "/var/log/proxy/jpuat_prox_error_log"
CustomLog "/var/log/proxy/jpuat_prox_access_log" common
ProxyRequests Off
<Proxy …Run Code Online (Sandbox Code Playgroud) 我正在尝试将所有访问 URL“ http://example.com/something ”的用户重定向到像“ http://answares.com/examples_com/something ”这样的 URL 。
我正在尝试使用这样的代码:
server {
listen 1.2.3.4:80;
server_name "example.com";
rewrite ^(.*) http://answares.com/examples_com$1 permanent;
}
Run Code Online (Sandbox Code Playgroud)
通过访问“ http://example.com/ ”我被重定向到“ http://answares.com/examples_com/ ”,但是通过访问“ http://example.com/something ”我被重定向到:“ http ://answares.com/something ”。
我尝试以不同的方式做到这一点,但我得到的最好的是:
http://answares.com/examples_com//something
Run Code Online (Sandbox Code Playgroud)
因为这两个斜线看起来很蹩脚。我在 Ubuntu 10.4 上使用 Nginx 0.7.65
我需要为 Nginx 编写一个重写规则,以便如果用户尝试转到旧图像 url:
/images/path/to/image.png
并且文件不存在,尝试重定向到:
/website_images/path/to/image.png
仅当图像存在于新 URL 中时,否则继续 404。我们主机上的 Nginx 版本还没有 try_files。
我正在将 WordPress 与 WP Super Cache 一起使用。我希望来自 Google 的访问者(包括所有国家/地区特定的引荐来源,如 google.co.in、google.co.uk 等)查看未缓存的内容。
我的 nginx 规则没有按照我想要的方式工作:
server {
server_name website.com;
location / {
root /var/www/html/website.com;
index index.php;
if ($http_referer ~* (www.google.com|www.google.co) ) {
rewrite . /index.php break;
}
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
if ($supercache_uri ~ ^(.+)$) …Run Code Online (Sandbox Code Playgroud) 我喜欢这个简单的重写规则:
/somefolder/mypage.aspx?myid=4343&tab=overview
Run Code Online (Sandbox Code Playgroud)
重定向到:
/folder/4343/overview/
Run Code Online (Sandbox Code Playgroud)
我寻找了一些解决方案,但没有一个真正奏效。
我试过:
rewrite ^/somefolder/mypage.aspx?myid=(.*)&tab=overview$ /folder/$1/overview permanent;
Run Code Online (Sandbox Code Playgroud)
和
rewrite ^/somefolder/mypage\.aspx\?myid=(.*)&tab=overview$ /folder/$1/overview permanent;
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我收到 404
(更简单的规则工作得很好..)
谢谢
我正在尝试以下重写规则:
location / {
index index.php index.html;
rewrite ^/test$ /test.php break;
}
Run Code Online (Sandbox Code Playgroud)
我还有:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/sites/default$fastcgi_script_name;
fastcgi_index index.php;
}
Run Code Online (Sandbox Code Playgroud)
普通的 php 文件工作正常,但似乎我的重定向只是下载我重定向到的 php 文件,而不是解析它。
我错过了什么?我通过将 fastcgi 内容放在第一个位置块中来加载文件,而不是下载,但它仍然没有解析为 php。
我想将请求转移到特定的子目录,到另一个根位置。如何?我现有的块是:
server {
listen 80;
server_name www.domain.com;
location / {
root /home/me/Documents/site1;
index index.html;
}
location /petproject {
root /home/me/pet-Project/website;
index index.html;
rewrite ^/petproject(.*)$ /$1;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} }
Run Code Online (Sandbox Code Playgroud)
也就是说,http://www.domain.com应该服务于 /home/me/Documents/site1/index.html 而http://www.domain.com/petproject应该服务于 /home/me/pet-Project/website /index.html - 似乎 nginx 在替换后重新运行所有规则,而http://www.domain.com/petproject只是提供 /home/me/Documents/site1/index.html 。