apache.htaccessmod-rewritejoomla3.0

Redirect url with ids range to another url using htaccess


I try to redirect user from Joomla plugins links that have specific IDs to the default admin page as following:

When user login in Joomla backend, he can reach this page of plugins:

https://www.example.com/administrator/index.php?option=com_plugins

Then if he wants to open a plugin with the id like 422 to edit it, he's to click on this link:

https://www.example.com/administrator/index.php?option=com_plugins&task=plugin.edit&extension_id=422

But instead of opening the plugin, I want the user to get redirected to this page:

https://www.example.com/administrator/index.php

To achieve this, I created a .htaccess in the folder administrator and placed the code at the end. So, I set a range of IDs of plugins that user cannot edit, but gets redirected. Please find the all content of .htaccess file as following:

# 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]

But does not work.


Solution

  • I create a .htaccess in the folder administrator and placed the code at the end.

    # 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]
    

    If the .htaccess file is inside the /administrator subdirectory then you need to remove administrator/ from the start of the RewriteRule pattern (1st argument), otherwise the rule will never match.

    In .htaccess, the RewriteRule pattern matches against a relative URL-path to the directory that contains the .htaccess file.

    In other words, it should look like this:

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

    Also, on Apache 2.4 you can use the QSD (Query String Discard) flag instead of appending an empty query string to remove the original query string.

    The preceding conditions that match the query string and plugin id are OK and should match the requested URL. (Although the word boundary \b elements are unnecessary.)

    Depending on what other directives you have, this rule should be near the top of the .htaccess file, not "at the end". Since you have used an absolute substitution string it would be more optimal to include these rules before your general canonical redirects (although this does assume you are not implementing HSTS).

    You are also missing the RewriteEngine On directive from the rules in question.

    So, it should look like this instead:

    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? [QSD,R=302,L]
    
    # 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 [QSD,R=302,L]
    
    # 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]
    

    Additional notes:

    1. I assume you have not implemented HSTS.

    2. I reversed the order of your two canonical redirects to reduce the number of redirects when requesting http://example.com/ (HTTP + non-www). But this does assume #1 above.

    3. Optimised the regex on the canonical redirects... no need to traverse and capture the entire URL-path when using the REQUEST_URI server variable.

    4. Removed the word boundary \b from the regex as this would seem unnecessary here.