.htaccess rewrite get parameter inside the URL - php

I'm having troubles to rewrite a get parameter inside the URL.
I need to replace to 0 the value or delete at all next request "filter=all" from urls across entire site.
Example of entire requst:
mysite.com/?filter=all&search_text=&type=xx&pagination=10&search_page=10&cs_loc_max_input=300&cs_loc_incr_step=1&goe_location_enable=off&submit=
I've tried:
RewriteRule ^filter=0$ filter=all&%{QUERY_STRING} [L,NC]
OR
RewriteCond %{QUERY_STRING} ^filter=all$
RewriteRule ^filter=0$ ?filter=all&%{QUERY_STRING}[L,NC]
OR
RewriteCond %{QUERY_STRING} ^(.*[&?]|)filter=(all|all2)([&?].*|)$
RewriteRule ^(.*)$ $1?filter=0&%{QUERY_STRING} [R=301,NC,L]
And few ways more, but nothing works as expected... Please, need a help.
Thanks

You can use:
RewriteCond %{QUERY_STRING} ^(.*)filter=all(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1filter=0%2 [R=301,L]
If you try to rewrite without redirect. Use [L] instead of [R=301,L].

Related

rewrite url with parameters in .htaccess

I have this incoming url where the last part is dynamic.
https://www.rishi.com/q/CuYuGy
and want to rewrite the url to below link.
https://rishi.com/test.php
where it gets the dynamic part and performs action
so far i have done this
RewriteCond %{HTTP_HOST} ^rishi.com/q/%{QUERY_STRING}$
RewriteRule ^$ {HTTP_HOST} ^rishi.com/test.php [L,R=301]
please help me resolve it.
Thanks
Try this
RewriteRule ^q/([a-zA-Z0-9-/]+)$ test.php?query_string=$1
RewriteRule ^q/([a-zA-Z0-9-/]+)/$ test.php?query_string=$1
Try this below htaccess code. It should help you
RewriteCond %{REQUEST_URI} ^/q/(.*)$
RewriteRule ^(.*)$ /test.php [L,R=301]

Why does one htaccess redirect work, but not the other even though they are formatted exactly the same?

I have the following lines in my htaccess file...
RewriteRule ^poolpage.php?poolid=9f30d1c77b91aca6318d179ad5df2b7 /poolpage.php?poolid=9f30d1c77b91aca6318d179ad5df2b7a [NC,L]
RewriteRule ^poolpage.php?poolid=c4899d49e0a26b47bb5d3fd85f2c429a /poolpage.php?poolid=c4899d49e0a26b47bb5d3fd85f2c429 [NC,L]
NOTE: Only difference in both is the last character of each URL.
The first one redirects fine. The second one does not.
Any ideas how this might happen?
You can't test query string in RewriteRule. Use instead:
RewriteCond %{QUERY_STRING} (?:^|&)poolid=9f30d1c77b91aca6318d179ad5df2b7(?:&|$) [NC]
RewriteRule ^poolpage\.php$ /poolpage.php?poolid=9f30d1c77b91aca6318d179ad5df2b7a [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)poolid=c4899d49e0a26b47bb5d3fd85f2c429a(?:&|$) [NC]
RewriteRule ^poolpage\.php$ /poolpage.php?poolid=c4899d49e0a26b47bb5d3fd85f2c429 [NC,L]

basic URL Rewriting in php issue

I have url coming on my web page like:
www.abc.com/product_list.php?id=12&pId=1&gpId=0
I want it to show like:
www.abc.com/product_list.php/12/1/0
i know this is very basic question but i am new in php.
your help will be really appreciated.
I did experiment like this but its not working: (in my htaccess file by googling)
RewriteRule ^product_list.php /product_list.php/id=$1/pId=$2/gpId=$3 [NC]
My .htaccessfile:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_USER_AGENT} (google|yahoo|msn|aol|bing) [OR]
RewriteCond %{HTTP_REFERER} (google|yahoo|msn|aol|bing)
RewriteRule ^([^/]*)/$ hamper-anastassia.php?$1 [L]
RewriteRule index.html$ /index.php
RewriteRule about.html$ /about.php
RewriteRule products.html$ /products.php
RewriteRule partners.html$ /partners.php
RewriteRule career.html$ /career.php
RewriteRule contact.html$ /contact.php
RewriteRule products_(.*)_(.*).html$ products.php?id=$2 [L]
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
You should remove the .php
A simple example to capture the first parameter.
RewriteRule ^products/([0-9]+)/?$ show_a_product.php?product_id=$1
More here
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Basically every pairing of brackets catches a variable, this is where the $1, $2, $3 comes from.
RewriteRule ^product_list.php/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
As Geoffrey mentions you could go one step further and remove the need for .php but that's up to you, e.g:
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
Meaning you could visit /product_list/12/1/0
Please remove/comment everything in your .htaccess file then type only these two statements
RewriteEngine on
RewriteRule ^products/([0-9]+)/([0-9]+)/([0-9]+)/?$ products.php?id=$1&pid=$2&gpid=$3
Your url should be like this:
/products/1/2/3
If your still getting and error check your mod_rewrite configurations

How to rewrite URL that contain parameters?

If user access this URL(http://domain.com/products/view-product.php?productID=21) in browser, it should show the page http://domain.com/new/view-product.php?pID=21
I have tried bellow code but it wont working, so please help me to resolve.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php
RewriteRule ^products/view-product.php?productID=21$ new/view-product.php?pID=21 [R=301,L]
products/view-product.php are physically exists in the server, still i want to rewrite it to the new page that i have designed now.
You can't match against the query string (everything after the ?) in a rewrite rule. Only the URI is used to match against that regular expression. You need to use the condition:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php\?productID=21($|\ |&)
RewriteRule ^products/view-product.php$ /new/view-product.php?pID=21 [R=301,L]
You can match query string using RewriteCond and %{QUERY_STRING} variable. Query string params didn't get to RewriteRule.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=21$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=21 [R=301,L]
Or if you want to redirect all productID's:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=([0-9]+)$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=%1 [R=301,L]
GET the value of productID and then redirect to the new URL.

htaccess multiple query string redirects to different urls

I am totally new to attempting redirects in htaccess but have a list of about 20 urls, all with query strings and all going to different urls.
I have managed to get a redirect using a query string working using RewriteCond and RewriteRule but when I add the other urls in the same format they all seem to redirect to the url in the first RewriteRule.
Its getting so frustrated as I have searched everywhere and tried so many ways to try and get this working. Hopefully someone on here can help me!
Here are a couple of the urls I need to redirect:
/store/index.php?search=flip flops >> http://www.stonemenswear.co.uk/menswear/flip-flops
/store/index.php?search=Boss+Orange+Shorts >> http://www.stonemenswear.co.uk/menswear/shorts
And here is the code I have got so far:
RewriteEngine on
RewriteCond "%{QUERY_STRING} search=flip flops"
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,N]
RewriteCond %{QUERY_STRING} search=Boss+Orange+Shorts
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/shorts? [R=301,N]
(plus the rest of the rewrites in the same format)
Each of these are getting redirected to the flip flops page!
Thanks in advance.
Wrong use of flag N you need L flag instead. Replace your code with this:
RewriteCond "%{QUERY_STRING} search=flip flops" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk//menswear/flip-flops/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts(&|$) [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L,NC]
You have some small syntax errors here.
RewriteCond %{QUERY_STRING} "^search=flip flops$" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts$ [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L]

Categories