How to retrieve data with htaccess Rewrite rule - php

please help, how do I retrieve data "page" from the following url:
mysite/start/search/indonesia/bali/18Jul2014/19Jul2014/100?page=2
if i use htaccess to direct?
This part of the line my htaccess
RewriteRule start/search/(.*)/(.*)/(.*)/(.*)/(.*)?pg=(.*)$ printdata.php?data=$1+$2+$3+$4+$5+$6 [QSA,L]
please help .. thank you response correction.

You need to match against the %{QUERY_STRING} or %{THE_REQUEST} variables. The query string (everything after the ?) isn't part of the URI that you match against in rewrite rules.
So:
RewriteCond %{THE_REQUEST} \ /+start/search/(.*)/(.*)/(.*)/(.*)/(.*)\?pg=(.*)$
RewriteRule ^ /printdata.php?data=%1+%2+%3+%4+%5+%6 [QSA,L]
Or
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^start/search/(.*)/(.*)/(.*)/(.*)/(.*)$ /printdata.php?data=$1+$2+$3+$4+$5+%1 [QSA,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]

301 redirect not working properly with htaccess

http://www.domain.com/thanks.php?orderid=MTI0NjAx
Above url should redirect to
http://www.domain.com/order-confirmation/MTI0NjAx
But now its redirecting to below url like...
http://www.domain.com/order-confirmation/MTI0NjAx?orderid=MTI0NjAx
I'm using this below code, but not working properly...
RewriteCond %{THE_REQUEST} /thanks\.php\?orderid=([^&\s]+) [NC]
RewriteRule ^ /order-confirmation/%1 [NC,R=301,L]
RewriteRule ^order-confirmation/([^/]+)$ /thanks\.php\?orderid=$1 [NC,L]
anyone help me, how do I do this?
You need to use ? in target URL to strip off previous query string:
RewriteCond %{THE_REQUEST} /thanks\.php\?orderid=([^&\s]+) [NC]
RewriteRule ^ /order-confirmation/%1? [R=301,L]
RewriteRule ^order-confirmation/([^/]+)$ /thanks\.php\?orderid=$1 [NC,L,QSA]
Starting with Apache 2.4 you can also use QSD (Query String Discard) flag to trip off previous query string.

.htaccess rewrite get parameter inside the URL

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

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 rewrite url

I've been tring for some time now with htaccess to rewrite this url
http://www.domain.com/folder/file.php?id=whatever
to
http://www.domain.com/?id=whatever
Any help would be much appreciated !
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ folder/file.php?id=$1 [QSA,L]
Must be something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ folder/file.php [QSA]
look for the exact syntax urself ... also notice the QSA flag ... actually it redirects the query string from called url to actual url ... also you can not match the part after '?' ie the query string in the regex ...

Categories