More .htaccess redirects

If we need to redirect more different URLs by query address to an unique address, or we need special rules, below, we are showing tips that can be used, with explanation.

  • [OR] menas, that next rules are also evaluated, if current rule was evaluated as false (pattern does not match with input)
  • %{QUERY_STRING} is, what is on the end of url after question mark - as shown below in example -if URL looks like https://domain.ext/hocijaka-cesta/?kontakt then the matching string to evaluate will be word "kontakt" (without quotes)
  • RewriteRule will rewrite URL by scheme. First is the rule for the original path - for example, we used only ^ - this means, that original url should be the protocol, then domain name, then RewriteBase.If RewriteBase is set to / (RewriteBase /), i tcan look then like this: http(s)://domain.ext/ - this rule is applied only to URL that begins on this pattern.Next item is path of new URL. Can be absolute (does not begin with slash) - for example http(s)://new-domain.ext/super-content, or relative (begins with slash)
  • L as last means, that evaluation ends with this rule.
  • R as redirect means, that the page will be redirected. There is used type 301 for permanent redirect, or 302 for temporary. 301 is good for searching services with stats (for example google) and the service uses stats for specific URLs. In case of using 301, the stats of new URL will be linked to stats of old URL. But, if 302 is used, then it will not happen (linking stats) and linking will be waiting, while there will be a new redirect with 301 code (this depends on service). Also 302 redirect is not stored in "cache" and can be easily changed unlike 301.
  • [NE] means, that URL should not be encoded - for example, space is converted to %20 and if in URL, there is a space, and NE is not specified then instead of sending "URL/nie co" the system wil send "URL/nie%20co" but if NE is set, the system will correctly send "URL/nie co".
  • In RewriteRule it is possible to use parenthesis around regular pattern and use content in target url. For example, if we want to change only part of url, we can use (as used in example below), the pattern (.*) - parenthesis will save the regular expression data to variable, and we will be then able to use it in 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 pomocou 302 presmerovania (docasne)
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/ and redirecting to #http(s)://domain.ext/kontakt/cz/ - after last slash, remaining URL part will be attached
RewriteCond %{QUERY_STRING} ^extra$
RewriteRule ^cz/kontakt/(.*)$ /kontakt/cz/$[NE,L,R=301]
?>

helpdesk

we are ready to serve

currently online

agents online 2 / 6

Platon Technologies

How can we help you?

We accept reports of problems or other inducements 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.

  • On the phone from 8:30 - 17:30
  • Sometimes even on weekends
  • Simple human approach
  • Complex problems solving
contact form

Give a chance to our online help-center. You can find there more than:

  • 6 categories
  • 59 manuals
  • 23 e-mail settings
  • 7 programming tips
go to counseling

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

Thanks for message

If necessary, we will contact you.

Send of message failed

Please contact us at helpdesk@platon.net.

NEWSLETTER

Subscribe to receive our news and current offers

Thanks for your subscribe

Subscribtion has failed