Search code examples
apache.htaccessmod-rewrite

Redirect page using .htaccess


Could you help me with redirect

https://example.com/dev/kampaaniad/?lang=ru

to

https://example.com/dev/ru/kampaaniad/

UPDATE: .htaccess code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=ru$
RewriteRule ^dev/kampaaniad/$ /dev/ru/kampaaniad/ [QSD,R=302,L]
</IfModule>

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /dev/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dev/index.php [L]
</IfModule>

# END WordPress

Solution

  • Try the following, using mod_rewrite, at the top of your .htaccess file in the document root:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^lang=ru$
    RewriteRule ^dev/kampaaniad/$ /dev/ru/kampaaniad/ [QSD,R=302,L]
    

    The RewriteRule pattern (ie. ^dev/kampaaniad/$) matches against the URL-path only, which is why we need a separate condition (RewriteCond directive) to match against the query string.

    The QSD flag is required to remove the original query string from the request.

    Alternatively, use backreferences to avoid repetition in the substitution string:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^lang=(ru)$
    RewriteRule ^(dev)/(kampaaniad/)$ /$1/%1/$2 [QSD,R=302,L]
    

    The $1 and $2 backreferences in the substitution string contain the captured subgroups from the RewriteRule pattern (ie. dev and kampaaniad/ respectively). The %1 backreference contains ru from the captured group in the preceding CondPattern.

    If the lang URL parameter value is variable in the query string then modify the RewriteCond directive in the last example to read:

    RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
    :
    

    To match any 2 character language code.

    Reference:


    UPDATE:

    ... redirect not working

    From the look of your complete .htaccess file in your updated question it's possible that your .htaccess file is inside the /dev subdirectory? With the directives above I naturally assumed you would be putting the .htaccess file in the document root (parent) directory.

    If the .htaccess file is located at /dev/.htaccess and not /.htaccess then you would need to modify the regex used in above RewriteRule as follows:

    RewriteCond %{QUERY_STRING} ^lang=ru$
    RewriteRule ^kampaaniad/$ /dev/ru/kampaaniad/ [QSD,R=302,L]
    

    The RewriteRule pattern matches the URL-path relative to the directory where the .htaccess file is located. So, ^kampaaniad/$ matches the URL /dev/kampaaniad/ when the .htaccess file is in the /dev subdirectory.

    You do not need the <IfModule> wrapper around these directives and you do not need to repeat the RewriteEngine directive, since this already occurs within the WordPress code block.

    To take advantage of the RewriteBase /dev/ directive defined later in the file, you can remove the path prefix on the substitution string, for example:

    :
    RewriteRule ^kampaaniad/$ ru/kampaaniad/ [QSD,R=302,L]
    

    And consequently, you can also remove the path prefix from the later rewrite:

    :
    RewriteRule . index.php [L]