Htaccess query string - string

I am trying to make:
http://www.specialisedorthoticservices.co.uk/image.php?object_type=detailed&image_id=140&window=popup
become this:
http://www.specialisedorthoticservices.co.uk
The result of the query no longer exists and the re-direct doesn't seem to work see code below:
RewriteCond %{REQUEST_URI} ^/image\.php$
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^(.*)$ http://www.specialisedorthoticservices.co.uk [R=301,L]

Remove the slash before image.php. In fact, why not just shorten it? To not append the query string you need a terminating question mark like so
RewriteEngine On
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^image\.php$ /? [R=301,L]

Related

Htaccess - RewriteRule, how to keep only the value part of a query string

I'm trying to rewrite an url from:
https://www.website.com/test/test-detail?name=abc
to:
https://www.website.com/test/abc
I can't seem to find a way to keep only the value part of the query string.
This is the code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=
RewriteRule ^(test)/(test-detail)/$ /test/$3 [R=301,L]
For the input:
https://website.com/test/test-detail/?name=abc
The result is:
https://website.com/test/?name=abc
Link to the example.
You can use this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=(.+)$
RewriteRule ^(test)/(test-detail)/$ /test/%1? [R=301,L]
Make sure to clear your browser's cache before you test this.

.htaccess - How to remove an end part of a url?

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

Remove Query String from Rootdomain

I know with a simple redirect one (Sub-)Domain or Folder to another i can get rid of a string like this
RewriteEngine On
RewriteRule (.*) http://www.domain.tld/? [R=301,L]
I know how to get rid of it when it is a simple file too.
But when it comes to the Rootdomain itself (http://domain.tld/?Stringwhatsoever), i am at a loss here. My last try used a modified version of a redirect I used to redirect files and folders around and that worked pretty nicely and also removed the query, but it ended up in a redirection error.
RewriteRule ^ http://domain.tld/? [L,NC,R=301]
So i have no clue how to get rid of Query Strings at urls without breaking it.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Or
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Reference :
-https://wiki.apache.org/httpd/RewriteQueryString

PHP RewriteRule detect if url contain "string" and replace

This may be a basic question regarding RewriteRule but I just counldn't make it work.
What I want to do is detect urls like:
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
mydomain/myapp/id/other/randomabc/perrito.php?param=somethingThatIdoUse
...
The part that I need to discart is from the /id/[all this]?param=somethingIdoUse and use the param if is possible or send the complete url as param so I can regex to get the param.
And have a rule that detect that /id/ exist and redirect to something like:
mydomain/myapp/other/manageRequest.php?params=somethingThatIdoUse
(the params I could get the whole url and strip it as string is no problem)
As well the application have different modules like:
mydomain/myapp/moduleOne/index.php
mydomain/myapp/moduleTwo/index.php
This have to keep working the same.
As far I've tried some of them like:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET /.*;.* HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://localhostdev/app/index.php %{REQUEST_URI}? [R=301,L]
RewriteEngine On
RewriteBase /spt/
RewriteCond %{REQUEST_FILENAME} !^id$ [NC]
RewriteRule ^(.*)$ index.php [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !/campaings/response\.php$
RewriteRule ^/something/(.*) /other/manageRequest.php? [L]
But nothing seamed to do kind of what I needed.
Thanks in advice
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
Here is an example using the above URL:
RewriteRule ^id/some/random/url.html/? /myapp/other/manageRequest.php [L,NC]
The query will be passed through unchanged to the substitution URL
well actually end up being really basic it worked with:
RewriteRule ^.*id.*$ handleRequest.php [NC,L]
I do get the params as they are sent!

Strip query_string

I want to get rid of some query string on my whole website (explicitly facebook shared/like query which usually begin with fb_action).
I thought about using .htaccess to do that.
I want this:
Link 1)
http://example.com/documents/de_desmarais_en_sirois?fb_action_ids=10151430962018298&fb_action_types=og.likes&fb_source=timeline_og&action_object_map={%2210151430962018298%22%3A157643874359578}&action_type_map={%2210151430962018298%22%3A%22og.likes%22}&action_ref_map=[]
to look like:
Link 2)
http://example.com/documents/de_desmarais_en_sirois
In my .htaccess, I added:
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) /$1? [R=301,L]
But, when I go on the original link, I am directed to my root folder example.com/pages.php, but I want to keep the first part of the URL which is example.com/documents/de_desmarais_en_sirois. I don't want to go to my root.
What should I modify/do?
I assume you've added those rules to the htaccess file in the /documents/ directory?
You'll need to remove the leading slash in your rule's target and add a rewrite base:
RewriteBase /documents/
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) $1? [R=301,L]
The query string match could be improved a bit though:
RewriteCond %{QUERY_STRING} (.*)(^|&)fb_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
RewriteCond %{QUERY_STRING} (.*)(^|&)action_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
To remove only the facebook query string parameters, if you have other parameters that you want to preserve.

Resources