AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[rewritecond](server)

Martin Hope
Adam Larsson
Asked: 2022-01-31 06:23:31 +0800 CST

Apache http 到 https 没有任何地址?

  • 1

正常的 http 到 https 重写可能如下所示:

RewriteEngine On
# RewriteCond %{HTTP_HOST} !^example.com$
# RewriteRule /.* https://example.com/ [R]

但是此代码将站点指定为“example.com”

无论如何要进行“全局”重写,因此无论是否有人打开example.com、broken.example.com 甚至example.Lom(只要 DNS 指向服务器),它都会简单地放一个 https在上面?

适用于拥有多个域名的服务器。

能够在任何地方使用相同的代码会很棒..

rewrite mod-rewrite rewritecond
  • 1 个回答
  • 42 Views
Martin Hope
iraqiboy90
Asked: 2021-12-29 13:26:46 +0800 CST

htaccess 重写规则不适用于文件扩展

  • 1

我在服务器上的同一文件夹中设置了两个域。

IE

dl.d123.com      @ /var/www/public_html/dl.domain123.com
dl.domain123.com @ /var/www/public_html/dl.domain123.com

我在 /var/www/public_html/domain123.com 中有一个 htaccess 文件,其中包含以下几行:

RewriteEngine On
#Rewrite URLs to one SSL domain
RewriteCond %{HTTP_HOST} ^dl\.d123\.com [NC]
RewriteRule ^(.*)$ https://dl.domain123.com/$1 [L,R=301,NC]

此规则适用于任何不包含文件扩展名的 URL

dl.d123.com/folder1/folder2 redirects to dl.domain123.com/folder1/folder2

但不适用于任何包含文件扩展名的 URL

dl.d123.com/folder1/folder2/index.html stays the same.

它不仅仅是 html 扩展。与 ie png 文件相同。

服务器设置为 nginx 作为代理。 nginx:80/443 -> apache:8080/8443

编辑:(为隐私/安全更改了域名和 IP)Nginx 配置:80

server {
    listen 123.123.123.123:80;  
    server_name dl.domain123.org  www.dl.domain123.org;

    access_log /usr/local/apache/domlogs/dl.domain123.org.bytes bytes;
    access_log /usr/local/apache/domlogs/dl.domain123.org.log full;
    error_log /usr/local/apache/domlogs/dl.domain123.org.error.log error;

    location / {
        location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh|webp)$ {
            root /home/s4h/dl.domain123.org;                    
            expires max;
            try_files $uri $uri/ @backend;
        }
        
        error_page 405 = @backend;
        error_page 500 = @custom;
        add_header X-Cache "HIT from Backend";
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @backend {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @custom {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ .*\.(php|jsp|cgi|pl|py)?$ {
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ /\.ht    {deny all;}
    location ~ /\.svn/  {deny all;}
    location ~ /\.git/  {deny all;}
    location ~ /\.hg/   {deny all;}
    location ~ /\.bzr/  {deny all;}
    location ~\.(ini|log|conf)$ {deny all;error_page 403 =404 / ;}

    disable_symlinks if_not_owner from=/home/s4h/dl.domain123.org;

    location /.well-known/acme-challenge {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }

    location /.well-known/pki-validation {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }
}

Nginx 配置:443

server {
    listen 123.123.123.123:443 ssl ;
    server_name dl.domain123.org  www.dl.domain123.org;
    
    access_log /usr/local/apache/domlogs/dl.domain123.org.bytes bytes;
    access_log /usr/local/apache/domlogs/dl.domain123.org.log full;
    error_log /usr/local/apache/domlogs/dl.domain123.org.error.log error;

    ssl_certificate      /etc/pki/tls/certs/dl.domain123.org.bundle;
    ssl_certificate_key  /etc/pki/tls/private/dl.domain123.org.key;
    ssl_protocols TLSv1.2;
    ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA!RC4:EECDH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS;
    ssl_prefer_server_ciphers   on;

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 60m;

    location / {
        location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh|webp)$ {
            root /home/s4h/dl.domain123.org;
            expires max;
            try_files $uri $uri/ @backend;
        }
        
        error_page 405 = @backend;
        error_page 500 = @custom;
        add_header X-Cache "HIT from Backend";
        add_header Strict-Transport-Security "max-age=31536000";
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @backend {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @custom {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ .*\.(php|jsp|cgi|pl|py)?$ {
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ /\.ht    {deny all;}
    location ~ /\.svn/  {deny all;}
    location ~ /\.git/  {deny all;}
    location ~ /\.hg/   {deny all;}
    location ~ /\.bzr/  {deny all;}
    location ~\.(ini|log|conf)$ {deny all;error_page 403 =404 / ;}

    disable_symlinks if_not_owner from=/home/s4h/dl.domain123.org;

    location /.well-known/acme-challenge {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }

    location /.well-known/pki-validation {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }
}

dl.d123.com Nginx,仅端口 80,未安装 SSL。

server {
    listen 123.123.123.123:80;  
    server_name dl.d123.com  www.dl.d123.com;

    access_log /usr/local/apache/domlogs/dl.d123.com.bytes bytes;
    access_log /usr/local/apache/domlogs/dl.d123.com.log full;
    error_log /usr/local/apache/domlogs/dl.d123.com.error.log error;

    location / {
        location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh|webp)$ {
            root /home/s4h/dl.domain123.com;                    
            expires max;
            try_files $uri $uri/ @backend;
        }
        
        error_page 405 = @backend;
        error_page 500 = @custom;
        add_header X-Cache "HIT from Backend";
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @backend {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location @custom {
        internal;
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ .*\.(php|jsp|cgi|pl|py)?$ {
        proxy_pass http://123.123.123.123:8181;
        include proxy.inc;
    }

    location ~ /\.ht    {deny all;}
    location ~ /\.svn/  {deny all;}
    location ~ /\.git/  {deny all;}
    location ~ /\.hg/   {deny all;}
    location ~ /\.bzr/  {deny all;}
    location ~\.(ini|log|conf)$ {deny all;error_page 403 =404 / ;}

    disable_symlinks if_not_owner from=/home/s4h/dl.domain123.com;

    location /.well-known/acme-challenge {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }

    location /.well-known/pki-validation {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }
}
.htaccess mod-rewrite rewritecond
  • 1 个回答
  • 82 Views
Martin Hope
alancc
Asked: 2021-11-11 22:09:17 +0800 CST

我应该在 RewriteCond 中转义斜杠“/”吗?

  • 1

我是否需要在 RewriteCond 中转义斜杠“/”?

目前我在.htaccess中写了以下规则:

RewriteCond %{QUERY_STRING} rp=/knowledgebase/
RewriteRule ^index\.php$ https://www.datanumen.com/knowledgebase/ [QSD,R=301,L,NC]

但是,这仅适用于https://www.datanumen.com/fi/customer/index.php?rp=/knowledgebase/7/How-to-order-the-full-version-of-DataNumen-Access之类的 URL -Repair.html&language=swedish,但不适用于https://www.datanumen.com/fi/customer/index.php?rp=%2Fknowledgebase%2F7%2FHow-to-order-the-full-version-之类的 URL of-DataNumen-Access-Repair.html&language=swedish

所以,我必须修改规则,如下所示:

RewriteCond %{QUERY_STRING} rp=/knowledgebase/ [OR]
RewriteCond %{QUERY_STRING} rp=%2Fknowledgebase%2F
RewriteRule ^index\.php$ https://www.datanumen.com/knowledgebase/ [QSD,R=301,L,NC]

但是我检查了https://serverfault.com/a/968916/280923并说“不需要转义斜杠(/) ”。所以我很困惑。

如果我需要考虑所有情况,即'/'的转义版本和非转义版本,那么应该总共有4种组合,我应该将它们全部添加为RewriteCond:

rp=/knowledgebase/
rp=%2Fknowledgebase%2F
rp=%2Fknowledgebase/
rp=/knowledgebase%2F
.htaccess mod-rewrite rewritecond
  • 1 个回答
  • 1049 Views
Martin Hope
David Scherfgen
Asked: 2021-04-20 06:43:15 +0800 CST

Apache:不要为某些不存在的文件(常规文件或符号链接)记录错误

  • 2

我想防止 Apache 为某些经常请求的不相关的不存在文件记录“文件不存在”错误,例如“apple-touch-icon-120x120.png”。这些日志条目使我的日志杂乱无章,很难看到“真正的”问题。这就是我想出的:

RewriteCond %{REQUEST_URI} ^/apple-touch-icon(-\d+x\d+)?(-precomposed)?\.png$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [redirect=404]

想法:如果请求的文件不是常规文件并且也不是符号链接,则 RewriteRule 将导致错误不被记录。那种作品。

现在我面临以下问题:如果我实际上创建了一个名为“apple-touch-icon-120x120.png”的符号链接,那么我的 RewriteRule 仍然会触发并且我得到 404。没有规则或者当我将文件设为常规文件时文件而不是符号链接,则该文件已正确提供。我认为通过使用!-l标志,只有在请求的文件不是常规文件并且它也不是符号链接时,我才能触发我的规则。但这似乎不起作用...

我在 Apache 的错误日志中没有看到任何可疑之处。我在这里可能做错了什么?

apache-2.2 symbolic-link mod-rewrite rewritecond
  • 1 个回答
  • 230 Views
Martin Hope
B3nny
Asked: 2020-10-30 06:31:59 +0800 CST

在 RewriteCond 上验证 QUERY_STRING 以获得 XSS 保护

  • 1

我想创建一个重写条件来验证 QUERY_STRING 以防止 XSS。

我的项目看起来像:

/ItemPage.jsp?itemId=item_12345_12

其中 item_12345 是必需的,“_{VERSIONNUMBER} 在查询字符串中是可选的。

过去,我为许多可能很关键的 javascript 部分创建了 RewriteCond。

例子:

RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} script [NC,OR]
RewriteCond %{QUERY_STRING} img [NC,OR]
RewriteCond %{QUERY_STRING} svg [NC,OR]
...
RewriteRule ^/ItemPage\.jsp?$ /StartPage.jsp [L,R=301]

在我发现使用许多新的可能的字符串进行过滤后,列表越来越多。现在我想重定向所有不使用此模式的请求来最小化规则。

RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} ^((?!itemId=[\w]).)* [NC,OR]
RewriteRule ^/ItemPage\.jsp?$ /StartPage.jsp [L,R=301]

但是如果我使用这个条件,我会一直被重定向。ItemPage.jsp 不需要 ItemId,但如果在查询字符串中设置了 ItemId,则需要 ItemId=item_12345 + 可选 ItemId=item_12345_12。

该规则在 Apache2.4 中是否可以过滤 XSS 代码?

rewrite apache-2.4 rewritecond
  • 1 个回答
  • 256 Views
Martin Hope
James Wong
Asked: 2020-09-29 21:01:59 +0800 CST

RewriteCond 中的 RewriteMap 不起作用(Apache)

  • 2

我有一个RewriteCond检查是否{QUERY_STRING}包含正确的版本号,如果没有,则将用户重定向到正确的版本。

例如,如果 v0.7 是最新的,http://localhost/?v=0.5则应将访问的用户重定向到,http://localhost/?v=0.7但由于某种原因,如果RewriteMap在条件下使用,它不起作用...

这有效

RewriteMap versions txt:/var/www/html/version.txt
RewriteCond "%{QUERY_STRING}" !^v=0.7
RewriteRule "^/$" "/?v=${versions:version}" [R,L]

这不

RewriteMap versions txt:/var/www/html/version.txt
RewriteCond "%{QUERY_STRING}" !^v=${versions:version}
RewriteRule "^/$" "/?v=${versions:version}" [R,L]

version.txt 的内容

##
##  version.txt -- rewriting map
##  The version number written here will be mapped to the URL
##
##
version 0.7
mod-rewrite apache-2.4 rewritecond rewritemap
  • 1 个回答
  • 470 Views
Martin Hope
uncovery
Asked: 2020-09-01 05:57:57 +0800 CST

使用 zonefile 或letsencrypt 配置 www.domain.com 和 domain.com 以始终转发到 domain.com

  • 0

我有一个示例应该始终导致https://example.com,即使用户输入www.example.com, https 与否。理想情况下,自动化的letsencrypt config https 配置文件应该可以工作。

  • 我试图删除 www 的 A 记录并使用 CNAME 来指向 example.com 或 @。这似乎没有任何影响,用户仍然最终访问 www。我不知道为什么。

这是我当前的配置:

<VirtualHost [IPV6]:80 IPV4:80>
    ServerAdmin [email protected]
    DocumentRoot /home/example/public_html
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog logs/example.error.log
    CustomLog logs/example.acccess.log common
    <Directory /home/example/public_html>
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    RewriteEngine on 
    RewriteCond %{SERVER_NAME} =example.com [OR] 
    RewriteCond %{SERVER_NAME} =www.example.com 
    RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent] 
</VirtualHost>

<VirtualHost [IPV6]:443 IPV4:443>
    ServerAdmin [email protected]
    DocumentRoot /home/example/public_html
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog logs/example.error.log
    CustomLog logs/example.acccess.log common
    <Directory /home/example/public_html>
        Options FollowSymLinks
        AllowOverride All
    </Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

但是,即使我将其添加到 :443 配置中, https: //www.example.com 也会抛出错误的证书:

    RewriteCond %{SERVER_NAME} =www.example.com 
    RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent] 

如何将 http 和 https 请求重定向到www.example.com到https://example.com?

cname-record mod-rewrite apache-2.4 rewritecond
  • 1 个回答
  • 172 Views
Martin Hope
stprysmqpwcuddakue
Asked: 2020-05-05 16:19:38 +0800 CST

apache .htaccess 在一个链接上重定向,但不是另一个

  • 0

我有这个 .htaccess 文件(这是 laravel 附带的文件。我所做的唯一更改是在 URL 中强制 https 和下面显示的代码:

 <IfModule mod_rewrite.c>
     <IfModule mod_negotiation.c>
         Options -MultiViews -Indexes
     </IfModule>

    ...

     RewriteEngine On

    ...

     # This block works perfectly
      RewriteCond %{REQUEST_URI} ^(/phpmyadmin.*)$
      RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
      RewriteRule ^(.*)$ /404 [R=301,L]

      # This block doesnt
      RewriteCond %{REQUEST_URI} ^(/processor.*)$
      RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
      RewriteRule ^(.*)$ /404 [R=301,L]

 </IfModule>

我想要实现的是我的一些页面被 IP 锁定。我已经通过上面的第一个块实现了这一点。

第二个块不适用于阻止所有 IP 地址,除非指定的 IP 地址完全相同。它仍然可供公众使用。

.htaccess apache-2.4 rewritecond
  • 1 个回答
  • 33 Views
Martin Hope
max stern
Asked: 2020-04-15 14:29:30 +0800 CST

Nginx:去掉 .html 后缀后,如何将所有请求重定向到新的 URL?

  • 1

我想优化旧的 Magento 商店系统的链接结构。到现在为止,在生成静态页面的时候,都会在对应的路径中添加一个.html后缀。由于较早的问题,甚至存在带有双后缀的路径(例如 .html.html)。现在我已禁用后缀并从数据库中删除了所有带有双后缀的路径,我想设置从旧 URL 到新 URL 的自动重定向。

为了保留现有的超链接和搜索引擎条目,我希望 Nginx 将对带有 .html 或 .html.html 后缀的页面的所有请求重定向到新路径。

要求:

example.org/banana.html
example.org/banana.html.html

应该重定向到:

example.org/banana

所以实际上我最好的猜测是:

location / {
  rewrite ^(.*?)\.html(\.html)?$ $1 permanent;
  try_files $uri $uri.html $uri.html.html $uri/ =404;
}

如何达到我的目标以及我需要将哪些重写规则添加到 nginx 配置中?

nginx regex rewritecond 301-redirect
  • 1 个回答
  • 550 Views
Martin Hope
Igor
Asked: 2016-12-09 13:20:24 +0800 CST

将 www 添加到域,但不添加到特定域

  • 0

我的.htaccess文件中有以下代码:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

上面的代码用于添加www到域,如果它没有www. 但是我有一个域,例如:(myloadbalancername-432566808.us-west-1.elb.amazonaws.com来自 AWS Elastic Load Balancer 的 DNS)并且该域不适用于www. 那么,除了特定域之外,如何www为所有域请求添加?

RewriteCond %{REQUEST_URI} !myloadbalancername
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

我尝试使用上面的代码,检查域名是否没有单词,然后检查域是否没有www,但没有成功。我是初学者,.htaccess所以我不知道我做错了什么。

rewrite .htaccess mod-rewrite rewritecond
  • 1 个回答
  • 474 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve