【问题标题】:Redirect url with ids range to another url using htaccess使用 htaccess 将具有 ids 范围的 url 重定向到另一个 url
【发布时间】:2022-10-04 19:08:17
【问题描述】:

我尝试将用户从具有特定 ID 的 Joomla 插件链接重定向到默认管理页面,如下所示:

当用户登录 Joomla 后端时,他可以访问这个插件页面: https://www.example.com/administrator/index.php?option=com_plugins

然后如果他想打开一个id像422这样的插件来编辑它,他要点击这个链接: https://www.example.com/administrator/index.php?option=com_plugins&task=plugin.edit&extension_id=422

但不是打开插件,我希望用户被重定向到这个页面: https://www.example.com/administrator/index.php

为此,我在文件夹管理员中创建了一个 .htaccess,并将代码放在最后。因此,我设置了一系列用户无法编辑但被重定向的插件 ID。 请找到 .htaccess 文件的所有内容如下:

# Canonical https/www
<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>


# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=\b(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])\b($|&)
RewriteRule ^administrator/index\.php$ https://www.example.com/administrator/index.php? [L,R=302]

# Redirect plug id from 425 to 10864:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=\b(42[5-9]|4[3-9][0-9]|[5-9][0-9]{2}|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|10[0-7][0-9]{2}|108[0-5][0-9]|1086[0-4])\b($|&)
RewriteRule ^administrator/index\.php$ https://www.example.com/administrator/index.php? [L,R=302]

但不起作用。

请帮忙。

【问题讨论】:

  • “少了点什么?” - 是的,一个正确表述的问题!您发布的代码没有任何“错误”(尽管没有必要使用边界断言一词),但这取决于您要执行的操作以及该代码的放置位置。请更新您的问题,详细说明您尝试重定向的请求以及在哪里确切地您已将该代码放入您的.htaccess 文件中。
  • 问题已编辑。
  • 这个 htaccess 正确吗?

标签: .htaccess joomla3.0


【解决方案1】:

我在文件夹administrator 中创建了一个.htaccess 并将代码放在最后。

# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{QUERY_STRING} (^|&)option=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])($|&)
RewriteRule ^administrator/index.php$ https://www.example.com/administrator/index.php? [L,R=302]

如果.htaccess 文件位于/administrator 子目录中,那么您需要从RewriteRule 的开头删除administrator/图案(第一个参数),否则规则永远不会匹配。

.htaccessRewriteRule图案与包含 .htaccess 文件的目录的相对 URL 路径匹配。

换句话说,它应该是这样的:

:
RewriteRule ^index.php$ https://www.example.com/administrator/index.php [QSD,R=302,L]

此外,在 Apache 2.4 上,您可以使用 QSD(查询字符串丢弃)标志而不是附加空查询字符串来删除原始查询字符串。

根据您拥有的其他指令,此规则应位于 .htaccess 文件的顶部附近,而不是“在末尾”。由于您使用了绝对替代string 最好包含这些规则您的一般规范重定向(尽管这确实假设您没有实现HSTS)。

您还缺少相关规则中的 RewriteEngine On 指令。

所以,它应该看起来像这样:

RewriteEngine On

# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{QUERY_STRING} (^|&)option=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])($|&)
RewriteRule ^index.php$ https://www.example.com/administrator/index.php? [L,R=302]

# Redirect plug id from 425 to 10864:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{QUERY_STRING} (^|&)option=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=(42[5-9]|4[3-9][0-9]|[5-9][0-9]{2}|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|10[0-7][0-9]{2}|108[0-5][0-9]|1086[0-4])($|&)
RewriteRule ^index.php$ https://www.example.com/administrator/index.php? [L,R=302]

# Canonical https/www
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

补充说明:

  1. 我假设你还没有实现HSTS

  2. 在请求http://example.com/(HTTP + 非 www)时,我颠倒了您的两个规范重定向的顺序以减少重定向的数量。但这确实假设上面的#1。

  3. 优化了规范重定向上的正则表达式...使用REQUEST_URI 服务器变量时无需遍历和捕获整个 URL 路径。

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多