这是一个关于 Apache 的 mod_rewrite的规范问题。
更改请求 URL 或将用户重定向到与他们最初请求的 URL 不同的 URL 是使用 mod_rewrite 完成的。这包括以下内容:
关于 Mod_Rewrite 规则你想知道但又不敢问的一切!
我怎样才能成为编写 mod_rewrite 规则的专家?
测试规则的地方
该htaccess的测试网站是玩弄你的规则,并测试他们的好地方。它甚至会显示调试输出,因此您可以查看匹配的内容和不匹配的内容。
我有一个带有 Apache 的专用服务器,我在上面设置了一些 VirtualHosts。我已经设置了一个来处理 www 域以及非 www 域。
我的 www 的 VH .conf 文件:
<VirtualHost *>
DocumentRoot /var/www/site
ServerName www.example.com
<Directory "/var/www/site">
allow from all
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
有了这个.htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将 www 重定向到非 www 版本?目前我将两个版本发送到同一个DocumentRoot并使用,.htaccess但我确定我必须能够在 VirtualHost 文件中做到这一点。
我有一堆重写规则,我必须将它们从 apache 移植到 nginx。
这是一个相当痛苦的过程,因为我无法查看我的重写规则和“如果”条件是否按我希望的那样工作。
Apache 确实对其重写模块进行了调试。我可以为 nginx 做什么?
我们使用 mod_proxy 模块作为负载均衡器配置了两个 Apache 服务器作为前端和 4 个 tomcat 服务器作为后端。现在,我们想从 mod_proxy 负载均衡器中排除单个 tomcat url。有什么办法或规则可以排除吗?
代理平衡器设置:
<Proxy balancer://backend-cluster1>
BalancerMember http://10.0.0.1:8080 loadfactor=1 route=test1 retry=10
BalancerMember http://10.0.0.2:8080 loadfactor=1 route=test2 retry=10
</Proxy>
Run Code Online (Sandbox Code Playgroud) 这已上传到我的 FTP 文件夹之一。我不熟悉 Apache,但仍然很好奇 - 有人能告诉我这个文件试图提交什么类型的卑鄙行为吗?谢谢!
RewriteEngine On
RewriteCond %{HTTP_REFERER} .*google.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*ask.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*yahoo.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*excite.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*altavista.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*msn.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*netscape.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*aol.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*hotbot.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*goto.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*infoseek.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*mamma.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*alltheweb.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*lycos.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*search.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*metacrawler.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*yandex.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} .*rambler.*$ [NC,OR] …Run Code Online (Sandbox Code Playgroud) 我正在 apache2 上为 wordpress 设置一个环境,在 ubuntu 12.04 的全新安装上。
为了让友好的 URLS 工作,我正在尝试设置 mod_rewrite。我遵循了我在网上找到的一些说明,并使用了 a2enmod。
现在。重新启动 apache 后,我想检查模块是否实际加载。
我找到的用于获取已加载模块列表的命令是:
apache2 -t -D 转储模块
但是,这会返回错误:
apache2:错误的用户名 ${APACHE_RUN_USER}
那么,我如何实际列出所有加载的模块,或者以其他方式检查是否已启用 mod_rewrite?
我正在尝试在不涉及任何代码的情况下实现跨域HTTP 访问控制。
我的 Apache(2) 服务器使用此块返回正确的访问控制标头:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS"
Run Code Online (Sandbox Code Playgroud)
我现在需要阻止 Apache 在浏览器发送HTTP OPTIONS请求(它存储在REQUEST_METHOD环境变量中)时执行我的代码,返回200 OK.
当请求方法为 OPTIONS 时,如何配置 Apache 以响应“200 OK”?
我试过这个mod_rewrite块,但访问控制头丢失了。
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
Run Code Online (Sandbox Code Playgroud) 我在其中一个 vhost 上进行了以下设置:
...<VirtualHost *:80>
ServerName cloud.domain.de
ServerAdmin webmaster@domain.de
ServerSignature Off
Alias "/.well-known/acme-challenge" "/var/www/domain.de/vh-www/htdocs/public/.well-known/acme-challenge"
<Directory "/var/www/domain.de/vh-www/htdocs/public/.well-known/acme-challenge">
Require all granted
ForceType 'text/plain'
</Directory>
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond %(REQUEST_URI) !/\.well\-known/acme\-challenge/?.*
RewriteCond %{HTTPS} off
# RewriteRule ^\.well-known/acme-challenge/([A-Za-z0-9-]+)/?$ - [L]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</ifmodule>...
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,当http://cloud.domain.de/.well-known/acme-challenge/访问url 时,mod_rewrite 不会重写 URL 。
我已经尝试了不同的方法,其中之一是上面注释掉的 RewriteRule,但似乎没有任何效果:服务器每次都将其重写为 https。
当我出于测试目的禁用重写时,我可以很好地访问别名 URL...
如何实现不被重写的特定 URL?
我在 Karaf 托管的应用程序前部署 Apache(Apache 和 Karaf 在不同的服务器上)。我希望 Apache 作为反向代理运行并隐藏部分 URL。
直接从应用服务器获取应用登录页面的 URL 是http://app-server:8181/jellyfish. 页面由在 Karaf 中运行的 Jetty 实例提供服务。当然,这种行为通常会被防火墙阻止,除了反向代理服务器。
在关闭防火墙的情况下,如果您点击此 URL,则 Jetty 会加载登录页面。浏览器的地址栏正确更改为http://app-server:8181/jellyfish/login?0,一切正常。
我想要的是http://web-server(即从根)映射到应用程序服务器上的 Jetty,应用程序的名称 ( jellyfish) 被抑制。例如,浏览器将更改为显示http://web-server/login?0在地址栏中,并且所有后续 URL 和内容都将使用网络服务器的域提供服务,而不会出现jellyfish混乱。
我可以使用以下配置(代码段)让 Apache 作为简单的反向代理运行:-
ProxyPass /jellyfish http://app-server:8181/jellyfish
ProxyPassReverse / http://app-server:8181/
Run Code Online (Sandbox Code Playgroud)
...但这需要浏览器的 URL 包含jellyfish并转到根 URL ( http://web-server) 会给出 404 Not Found。
我花了很多时间尝试使用mod_rewrite和不使用它的[P]标志来解决这个问题,但没有成功。然后我尝试了该ProxyPassMatch指令,但我似乎也无法完全正确。
这是当前配置,已加载到/etc/apache2/sites-available/Web 服务器上。请注意,有一个本地托管的图像目录。我还保留了mod_rewrite 代理漏洞利用保护,并抑制了一些mod_security会产生误报的规则。
<VirtualHost *:80> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用有趣的 Apache mod_rewrite 设置转换反向代理以使用 Nginx(由于外部问题,我们正在从 Apache 迁移到 Nginx,除了这部分外,大多数一切都正常)。
我最初的设置是读取 HTTP cookie(由某些应用程序设置)并根据其值将反向代理定向到不同的后端。它是这样的:
RewriteCond %{HTTP_COOKIE} proxy-target-A
RewriteRule ^/original-request/ http://backend-a/some-application [P,QSA]
RewriteCond %{HTTP_COOKIE} proxy-target-B
RewriteRule ^/original-request http://backend-b/another-application [P,QSA]
RewriteRule ^/original-request http://primary-backend/original-application [P,QSA]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Nginx 实现相同的目标,我的初始配置是这样的(其中“proxy_override”是 cookie 的名称):
location /original-request {
if ($cookie_proxy_override = "proxy-target-A") {
rewrite . http://backend-a/some-application;
break;
}
if ($cookie_proxy_override = "proxy-target-B") {
rewrite . http://backend-b/another-application;
break;
}
proxy_pass http://primary-backend/original-application;
}
Run Code Online (Sandbox Code Playgroud)
但它没有。我试图通过编写主代理重定向到基于${cookie_proxy_override}的内容来查看 Nginx 是否可以读取我的 cookie ,我可以看到它读取的内容很好,但ifs 似乎总是失败。
根据 Rikih 的回答,我的下一次尝试是这样的:
location /original-request {
if ($http_cookie ~ "proxy-target-A") …Run Code Online (Sandbox Code Playgroud) mod-rewrite ×10
apache-2.2 ×8
linux ×2
mod-proxy ×2
nginx ×2
redirect ×2
security ×2
.htaccess ×1
301-redirect ×1
apache-2.4 ×1
cookie ×1
http ×1
lamp ×1
mod-alias ×1
redirection ×1
tomcat ×1
virtualhost ×1
web-server ×1