More .htaccess redirects

When you need to redirect several different URLs distinguished by their query string to a single address, or you need special rules, here are some tips you can use, with an explanation of each one.

  • [OR] means that the next rule is evaluated as well if the current one came out false, that is if the pattern did not match the input.
  • %{QUERY_STRING} is what comes at the end of the URL after the question mark. In the example below, if the URL looks like https://domain.ext/any-path/?kontakt, then the string to be matched is the word "kontakt" (without the quotes).
  • RewriteRule rewrites the URL according to a pattern. The first part is the pattern for the original path. In the example we use only ^, which means the original URL is the protocol, then the domain name, then the RewriteBase. If RewriteBase is set to / (RewriteBase /), it can look like this: http(s)://domain.ext/ - and the rule then applies only to URLs that begin with that. The second part is the path of the new URL. It can be absolute, such as http(s)://new-domain.ext/super-content, or relative, in which case it begins with a slash.
  • L, for last, means that the evaluation ends with this rule.
  • R, for redirect, means that the request will be redirected. Type 301 is used for a permanent redirect, 302 for a temporary one. A 301 is what search engines expect for an address that has moved for good: they transfer the signals of the old URL to the new one. With a 302 that does not happen, and the search engine keeps the old address until it sees a 301 instead, although the exact behaviour differs between them. A 302 is also not cached, so it is easier to change later than a 301.
  • [NE] means that the URL must not be encoded. A space, for example, is normally turned into %20, so without NE the system would send "URL/nie%20co" instead of "URL/nie co". With NE set, it sends "URL/nie co" correctly.
  • In RewriteRule you can put parentheses around part of the pattern and reuse what they captured in the target URL. If you want to change only a part of the URL, use the pattern (.*) as in the example below. The parentheses store the matched text, and you can then insert it into the target URL with $1.

<?php
#URL, that match is http(s)://domain.ext/?kontakt(y) - redirect to http(s)://domain.ext/kontakt/
RewriteCond %{QUERY_STRING} ^kontakt$ [OR]
RewriteCond %{QUERY_STRING} ^kontakty$
RewriteRule ^ /kontakt/ [L,R=301]

#Redirecting from http(s)://domain.ext/?sidlo to http(s)://domain.ext/kontakt/#sidlo
RewriteCond %{QUERY_STRING} ^sidlo$
RewriteRule ^ /kontakt/#sidlo [NE,L,R=301]

#Redirecting from http(s)://domain.ext/kontakt/bratislava?pobocka to
#http(s)://domain.ext/kontakt/#bratislava using a 302 redirect (temporary)
RewriteCond %{QUERY_STRING} ^pobocka$
RewriteRule ^kontakt/bratislava /kontakt/#bratislava [L,R=302]

#Redirecting from http(s)://domain.ext/kontakt/sidlo to http(s)://domain.ext/kontakt/
RewriteRule ^kontakt/sidlo /kontakt/ [L,R=301]

#Redirecting everything that begins with http(s)://domain.ext/cz/kontakt/ to
#http(s)://domain.ext/kontakt/cz/ - whatever follows the last slash is appended
RewriteCond %{QUERY_STRING} ^extra$
RewriteRule ^cz/kontakt/(.*)$ /kontakt/cz/$1 [NE,L,R=301]
?>

helpdesk

we are ready to help

currently online

agents online 2 / 6

Platon Technologies

How can we help you?

We accept problem reports and any other feedback related to the use of our products and services.

 

helpdesk@platon.net +421 222 111 321

In case you need any help, contact us by e-mail or by phone.

  • By phone from 8:30 - 17:30
  • Sometimes even on weekends
  • Simple human approach
  • Even complex problems solved
contact form

Give our online help center a try. You will find more than:

  • 6 categories
  • 60 manuals
  • 23 e-mail settings
  • 8 programming tips
go to help center

Questions about our products or any specific request? Contact us.

We are checking whether the message is spam. Please wait...

Thanks for your message

If necessary, we will contact you.

Sending the message failed

Please contact us at helpdesk@platon.net.

NEWSLETTER

Thanks for subscribing

Subscription has failed