标签: mod-rewrite

在 Apache 中重定向、更改 URL 或将 HTTP 重定向到 HTTPS - 关于 mod_rewrite 规则你想知道但又不敢问的一切

这是一个关于 Apache 的 mod_rewrite的规范问题

更改请求 URL 或将用户重定向到与他们最初请求的 URL 不同的 URL 是使用 mod_rewrite 完成的。这包括以下内容:

  • 将 HTTP 更改为 HTTPS(或相反)
  • 将不再存在的页面请求更改为新的替换。
  • 修改 URL 格式(例如 ?id=3433 为 /id/3433 )
  • 基于浏览器,基于引用者,基于月亮和太阳下的任何可能呈现不同的页面。
  • 任何你想弄乱 URL 的东西

关于 Mod_Rewrite 规则你想知道但又不敢问的一切!

我怎样才能成为编写 mod_rewrite 规则的专家?

  • mod_rewrite 规则的基本格式和结构是什么?
  • 我需要牢牢掌握正则表达式的什么形式/风格?
  • 编写重写规则时最常见的错误/陷阱是什么?
  • 什么是测试和验证 mod_rewrite 规则的好方法?
  • 我应该注意 mod_rewrite 规则对 SEO 或性能的影响吗?
  • 是否存在 mod_rewrite 看起来是适合这项工作的正确工具但不是的常见情况?
  • 有哪些常见的例子?

测试规则的地方

htaccess的测试网站是玩弄你的规则,并测试他们的好地方。它甚至会显示调试输出,因此您可以查看匹配的内容和不匹配的内容。

mod-rewrite redirect redirection 301-redirect apache-2.2

275
推荐指数
5
解决办法
11万
查看次数

在 Apache VirtualHost 中重定向 URL?

我有一个带有 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 文件中做到这一点。

mod-rewrite redirect virtualhost apache-2.2

65
推荐指数
2
解决办法
25万
查看次数

在配置文件中调试 nginx 重写规则的最佳方法?

我有一堆重写规则,我必须将它们从 apache 移植到 nginx。

这是一个相当痛苦的过程,因为我无法查看我的重写规则和“如果”条件是否按我希望的那样工作。

Apache 确实对其重写模块进行了调试。我可以为 nginx 做什么?

linux mod-rewrite nginx lamp apache-2.2

52
推荐指数
3
解决办法
8万
查看次数

如何排除 Apache Mod_proxy 的 URL?

我们使用 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)

linux mod-rewrite tomcat mod-proxy apache-2.2

34
推荐指数
3
解决办法
5万
查看次数

可疑的 .htaccess 文件

这已上传到我的 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)

security mod-rewrite .htaccess apache-2.2

20
推荐指数
2
解决办法
746
查看次数

如何测试是否启用了 mod_rewrite?

我正在 apache2 上为 wordpress 设置一个环境,在 ubuntu 12.04 的全新安装上。

为了让友好的 URLS 工作,我正在尝试设置 mod_rewrite。我遵循了我在网上找到的一些说明,并使用了 a2enmod。

现在。重新启动 apache 后,我想检查模块是否实际加载。

我找到的用于获取已加载模块列表的命令是:

apache2 -t -D 转储模块

但是,这会返回错误:

apache2:错误的用户名 ${APACHE_RUN_USER}

那么,我如何实际列出所有加载的模块,或者以其他方式检查是否已启用 mod_rewrite?

mod-rewrite apache-2.2

18
推荐指数
4
解决办法
7万
查看次数

在 Apache 中对 HTTP OPTIONS 请求返回“200 OK”

我正在尝试在不涉及任何代码的情况下实现跨域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)

security mod-rewrite http apache-2.2

17
推荐指数
3
解决办法
5万
查看次数

apache2.4 mod_rewrite 排除特定别名 directroy/uri

我在其中一个 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?

mod-rewrite mod-alias apache-2.4

16
推荐指数
2
解决办法
2万
查看次数

反向代理中的Apache 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)

mod-rewrite web-server reverse-proxy mod-proxy apache-2.2

15
推荐指数
1
解决办法
8万
查看次数

使用 cookie 控制 Nginx 代理目标?

我正在尝试使用有趣的 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 nginx reverse-proxy cookie

14
推荐指数
2
解决办法
4万
查看次数