Redirect htaccess php - .htaccess

I want to redirect this page with htaccess
products_filter.php?f16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
to
products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x
i tried this (and many other ways)
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]
what am i doing wrong here?

Problem is using $ (end of input) in this regex:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
Since your query string is: 16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
Change that line to:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla(&|$)
Update:
Looking at your question I realize that you are using quite a few special characters that need to be escaped.
RewriteCond %{QUERY_STRING} ^(f16%5B0%5D)=bla\+bla\+bla(?:&(.*)|$) [NC]
RewriteRule ^(products_filter\.php)$ /$1?%1=bla+bla&%2 [L,R=301,NE]
PS: It is important to use NE flag here. Otherwise %5B and %5D will be further encoded by Apache.

Try this code :
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]

Related

URL redirect with query strings - htaccess

This is really bugging me, I have followed a few tutorials but just can't get anywhere. I'm trying to do a 301 redirect from:
/webpage-mackay.php?wp=Mission
to:
http://domain.org.au/webpage.php?wp=Mackay%20Mission
I have attempted writing like this:
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^/webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
and:
RewriteCond %{REQUEST_URI} ^/webpage-mackay.php$
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^(.*)$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
But the result is:
http://domain.org.au/webpage.php?wp=Mission
Am I missing something? I have used this and this as a reference
I see 2 problems with your first attempt : there is no need for the leading slash in the RewriteRule, and the %20 doesn't work as "%" is a special character.
Here what you can try :
# Solution 1 : with a space character in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\ Mission [R=301,L]
# Solution 2 : with the %20 in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\%20Mission [R=301,L,NE]

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!

.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

Multipart htaccess redirect

Hey guys I'm having a bit of trouble getting my htaccess to redirect properly and was hoping for some help.
I'm expecting DEV-domain.com?CampID=AB12345 to redirect to
http://DEV-www.domain.com/landing/external-marketing/direct-mail/AB?CampId=AB12345
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=
RewriteRule (\w{2})(\w{5})$ http://DEV-www\.domain\.com/landing/external-marketing/direct-mail/$1?CampId=$1$2 [R=301,L]
Unfortunetly I can't get it working for some reason?
Because the RewriteRule matching is meant for the url path, not query strings. Try this:
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=(\w{2})(\w{5})
RewriteRule .* http://DEV-www.domain.com/landing/external-marketing/direct-mail/%1?CampId=%1%2 [R=301,L]
also you don't need to escape dots . in the target url, only in matching patterns. And be aware that if you decide to make your target url CampID instead of CampId, you need to put in another condition:
RewriteCond %{REQUEST_URI} !^/landing/external-marketing/direct-mail/
to avoid an infinite redirect as a target with CampID would match your RewriteCond rule...

.htaccess mod_rewrite: rewriting querystring to path

How could I use a rewrite to change:
/?tag=foo
To:
/tag/foo
I tried:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]
But it did not work.
Try the following:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]
Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?
RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]
Then you can rewrite that requests back internally without conflicts:
RewriteRule ^tag/(.*) index.php?tag=$1 [L]
I was trying to convert from a URL like this:
http://java.scandilabs.com/faq?key=Contents_of__gitigno
To a URL like this:
http://scandilabs.com/technology/knowledge/Contents_of__gitigno
Andrew's response above worked for me with the addition of a question mark at the end to discard the original query string:
RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]
Note if you're on apache 2.4 or higher you can probably use the QSD flag instead of the question mark.

Resources