Bitnami Apache 重写重写 Cond 和规则

Bitnami Apache rewrite Rewrite Cond and Rule

提问人:Josh Hawkes 提问时间:11/2/2023 最后编辑:RavinderSingh13Josh Hawkes 更新时间:11/5/2023 访问量:65

问:

我真的很感激在重写一些东西方面得到一些帮助。 我已将一个站点移动到子域,但现在指向该站点的链接不起作用。

之前的链接是:https://www.domainname.com/folder/folder/folder/folder/show?elem=icon_file9393

它需要重定向到并添加子域的新链接是:https://www.subdomain.domainname.com/folder/folder/folder/folder/show?elem=icon_file9393

文件夹名称是动态的 show?elem= 将始终存在,但icon_file9393是动态的

任何帮助将不胜感激

这是我目前在 /opt/bitnami/apache/conf/vhost/wordpress-https-vhost.conf 中尝试的

<VirtualHost 127.0.0.1:443 _default_:443>
  ServerName www.example.com
  ServerAlias *
  SSLEngine on
  SSLCertificateFile “###########”
  SSLCertificateKeyFile “##########”
  DocumentRoot /opt/bitnami/wordpress
  # BEGIN: Configuration for letsencrypt
  Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
  # END: Configuration for letsencrypt
  # BEGIN: Support domain renewal when using mod_proxy without Location
  <IfModule mod_proxy.c>
    ProxyPass /.well-known !
  </IfModule>
  # END: Support domain renewal when using mod_proxy without Location
  # BEGIN: Enable non-www to www redirection
  RewriteEngine On

  RewriteEngine On
  RewriteCond %{QUERY_STRING} (^|&)elem=[^&]+(&|$) [NC]
  RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnmae\.com$ [NC]
  RewriteRule ^[^/]+/[^/]+/[^/]+/[^/]+/show/?$  https://www.subdomain.domainname.com%{REQUEST_URI} [R=301,L,NC,NE]
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=permanent,L]
  # END: Enable non-www to www redirection
  <Directory "/opt/bitnami/wordpress">
    Options -Indexes +FollowSymLinks -MultiViews
    AllowOverride None
    Require all granted
    # BEGIN WordPress fix for plugins and themes
    # Certain WordPress plugins and themes do not properly link to PHP files because of symbolic links
    # https://github.com/bitnami/bitnami-docker-wordpress-nginx/issues/43
    RewriteEngine On
    RewriteRule ^bitnami/wordpress(/.*) $1 [L]
    # END WordPress fix for plugins and themes
    # BEGIN WordPress
    # https://wordpress.org/support/article/htaccess/#basic-wp
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
  </Directory>
  Include "/opt/bitnami/apache/conf/vhosts/htaccess/wordpress-htaccess.conf"
  # BEGIN: Support domain renewal when using mod_proxy within Location
  <Location /.well-known>
    <IfModule mod_proxy.c>
      ProxyPass !
    </IfModule>
  </Location>
  # END: Support domain renewal when using mod_proxy within Location
</VirtualHost>

这是 /opt/bitnami/apache/conf/vhost/htaccesss/wordpress-htaccess.conf:

<Directory "/opt/bitnami/wordpress/wp-content/plugins/akismet">
  # Only allow direct access to specific Web-available files.
  
  # Apache 2.2
  <IfModule !mod_authz_core.c>
  Order Deny,Allow
  Deny from all
  </IfModule>
  
  # Apache 2.4
  <IfModule mod_authz_core.c>
  Require all denied
  </IfModule>
  
  # Akismet CSS and JS
  <FilesMatch "^(form\.js|akismet(-frontend|-admin)?\.js|akismet(-admin)?(-rtl)?\.css|inter\.css)$">
  <IfModule !mod_authz_core.c>
  Allow from all
  </IfModule>
  
  <IfModule mod_authz_core.c>
  Require all granted
  </IfModule>
  </FilesMatch>
  
  # Akismet images
  <FilesMatch "^(logo-(a|full)-2x\.png|akismet-refresh-logo\.svg|akismet-refresh-logo@2x\.png|arrow-left\.svg)$">
  <IfModule !mod_authz_core.c>
  Allow from all
  </IfModule>
  
  <IfModule mod_authz_core.c>
  Require all granted
  </IfModule>
  </FilesMatch>
</Directory>
正则表达式 apache .htaccess mod-rewrite url-rewriting

评论

0赞 anubhava 11/2/2023
您是否也想被重定向到?https://www.domainname.com/https://www.subdomain.domainname.com/
1赞 Josh Hawkes 11/2/2023
不,只有 4 / 并以 elem=icon?* 结尾的域结构需要重定向。谢谢

答:

0赞 anubhava 11/3/2023 #1

您可以在以下根目录的 .htaccess 中使用此重定向规则:domain.com

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)elem=[^&]+(&|$) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainname\.com$ [NC]
RewriteRule ^/?[^/]+/[^/]+/[^/]+/[^/]+/show/?$ https://www.subdomain.domainname.com%{REQUEST_URI} [R=301,L,NC,NE]

查询字符串将自动转发到新的重定向 URL。

评论

0赞 Josh Hawkes 11/3/2023
我想我也肯定把它放在了正确的地方,因为我刚刚测试了它正在工作:RewriteCond %{REQUEST_URI} ^/example/(.*)$ RewriteRule ^(.*)$ http://google.com [P,L]
0赞 anubhava 11/3/2023
确保这是行下方的最上层规则。RewriteEngine On
0赞 Josh Hawkes 11/3/2023
是的,这是第一条规则。我错过了别的什么吗......?
0赞 anubhava 11/3/2023
您在浏览器中输入的确切 URL 是什么来测试这一点?此外,编辑您的问题,使用此建议的规则显示您最新的 .htaccess。
0赞 anubhava 11/3/2023
我已经使用您提供的 URL 在我的本地 Apache 上测试了此规则,并且它被重定向得很好。