0

I would like to redirect the following urls from:

http://example.com/index.php/item123-detail?tmpl=component&format=pdf

to:

http://example.com/index.php/item123-detail

In essence removing the "?tmpl=component&format=pdf" from all urls.

I have tried multiple different examples from other Stack questions without luck so far. Any help would be much appreciated. Thank you.

usermee
  • 11
  • 2

2 Answers2

0

This part of URL ?tmpl=component&format=pdf called QUERY_STRING and if you want to remove it from any request you could do several scenarios like putting this code at main directory .htaccess this :

RewriteEngine On
RewriteCond %{THE_REQUEST} \?
RewriteRule ^(.*)$   /$1? [L,R=301]

So , by the code above you will be able to remove even request with ? only.

If you want to match only this query string and keep others , let me know to give you another scenario with another condition RewriteCond %{QUERY_STRING}

Ok , to match only this query string , replace the code with :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=component&format=pdf$
RewriteRule ^(.*)$   /$1? [L,R=301]

And if tmpl & format values not fixed and come only into letters, replace the code with:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=([a-zA-Z]+)&format=([a-zA-Z]+)$
RewriteRule ^(.*)$   /$1? [L,R=301]
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • Thank you Mohammed. – usermee Mar 05 '18 at 14:57
  • Hi Mohammed, I have come across an issue where I actually do need it to be specific to this query as this now breaks another similar query which uses a ?. Please would you be so kind as to assist once more to match only this query string. Thank you! – usermee Mar 07 '18 at 09:13
-1

Solution found on another post as follows:

remove query string from end of url URL using .htaccess

RewriteCond %{QUERY_STRING} "post_type=" [NC]

RewriteRule (.*) /$1? [R=301,L]

usermee
  • 11
  • 2