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]
Related
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.
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
I am having difficulty getting a proper redirect from .htaccess. The redirect that I am attempting is the following:
Original URL:
http://www.mysite.com/index.php/contact-us?id=12
Should Direct to:
http://www.mysite.com/index.php/new-contact-request-form
I have attempted a variety of variations on the following samples with no luck. Any ideas on where I’m going wrong?
Example 1:
Redirect /index.php/contact-us?id=12 http://www.mysite.com/index.php/new-contact-request-form
Example 2:
RewriteCond %{QUERY_STRING} ^index.php/contact-us?id=12$
RewriteRule ^index\.php\/new\-contact\-request\-form$ http://www.mysite.com/? [R=301,L]
Example 3:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com\/index\.php\/contact\-us\?id\=12$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com\/index\.php\/new\-contact\-request\-form" [R=301,L]
URL structure:
%{QUERY_STRING} variable contains the url query string (which is in our case id=12),
in the other hand, RewriteRule operates on the url path only (not including the query string),
so basically , the code below check if the query string is equal to id=12 and if the url path is equal to index.php/contact-us , if so, a redirection will take place
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=12$
RewriteRule ^index\.php/contact\-us$ /index.php/new-contact-request-form? [R=301,L]
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!
Hello I have been searching the web for an answer to this but I can not find it. I am looking to use htaccess to strip a specific parameter from the url.
For example, I have the following urls:
www.mysite.com/product.php?product_id=123&session=sdfs98d7fs9f8d7
www.mysite.com/anotherurl.php?session=12312341341&someotherparam=123
I would need them to 301 redirect to:
www.mysite.com/product.php?product_id=123
www.mysite.com/anotherurl.php?someotherparam=123
Note the urls above are just examples. Ill need the session param removed from any and all urls no matter how many params are part of the url or where session is located.
I think I need a way to the url string up to session=blah and the url string after session=blah, then combine both parts and redirect to the new url.
Any help is greatly appreciated, thanks.
UPDATED
In short, what you want is just to remove the key-value pair session=xx from the query.
Here is an option:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)?&?session=[^&]+&?(.*)? [NC]
RewriteCond %{REQUEST_URI} /(.*)
RewriteRule .* %3?%1%2? [R=301,L]
I found a solution on another site
# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)&session=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)&session=[0-9a-z]+$|^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
not tested, but should work:
RewriteCond %{QUERY_STRING} product_id=([0-9]+)
RewriteRule .* product.php?product_id=%1