PHP RewriteRule detect if url contain "string" and replace - .htaccess

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!

Related

.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 parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [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

.htaccess remove GET completely

I have dozens of redirects from an old page e.g. index.php?mode=1,2,3,0 and I want to get rid of all GET Params because the new page is anyways just plain html.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} mode=17,0,0,0,0$
RewriteRule (.*) /big-mamas-house/ [R=301,L]
I thought removing (.*) would already do the trick but then the rule is not applied anymore according to:
http://htaccess.madewithlove.be/
Your rule can simplified to:
RewriteCond %{QUERY_STRING} ^mode=17,0,0,0,0$
RewriteRule ^index\.php$ /big-mamas-house/? [R=301,L,NC]
? in the end is needed to strip off any previous query string.

.htaccess remove specific get parameter from url

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

Resources