How to rewrite URL that contain parameters? - php

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.

Related

How do I redirect an asp url with query string to php url

There is an old link coming to my site that ends with sc_purchase_a.asp?discode=foo&ProID=bar. I want to redirect that to /courses/shop/4u.php?discode=foo. This is what I've done so far, but its not working.
RewriteRule ^sc_purchase_a.asp?discode=(\w+)&ProdID=(\w+)/?$ /courses/shop/4u.php?discode=$1 [QSA]
This doesnt seem to work.
Based on starkeen's answer, I came up with this, but it is not working and now gives me 500 errors.
RewriteCond %{THE_REQUEST} /sc_purchase_a\.asp\?discode=([^&]+)&ProID=.+ [NC]
RewriteRule ^ /courses/shop/4u.php?discode=%1 [L,R]
QueryString is not part of match in RewriteRules pattern. To manupulate query string ,you need to use a RewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.asp\?code=([^&]+)&id=.+ [NC]
RewriteRule ^ /page.php?code=%1 [L,R]

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

.htaccess mod rewrite help needed

I have written this little piece of code. I am very new to this so i am not sure it is all correct. but basically it lets me access urls with the php extension. When people get on the site they are being redirected from the geo ip page to the correct language which like looks like this
main.php?lang=uk or nl or en or eu etc.
right now i can also use it like this
main/?lang=uk or nl or en or eu etc.
I would like to be able to to also remove the variable in the url ?lang=uk.
How would i do this. My .htaccess code is below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
DirectorySlash On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
</IfModule>
Thanks too anyone willing to help.
The first argument of RewriteRule does match anything after the domain name and prefix* and before any query string if that exists. In http://localhost/this/is/my/test?lang=123 with a .htaccess file in the this/is/ directory it would match my/test. To match a query string, you have to use the %{QUERY_STRING} variable.
If the second argument (the rewritten url) of RewriteRule does not contain a query string, it will automatically append the query string of the original url to the new url.
In the code below I use %{THE_REQUEST}. This is the string that is used to make the request for a page. It is in the form of GET /my/resource?query=string HTTP/1.1. It does not change when rewriting things, which makes it useful to prevent infinite loops.
In your php file make sure that the language is read from a cookie instead of a get variable, then do the following:
#Set cookie for language
RewriteCond %{QUERY_STRING} ^(.*?)&?lang=([^&]+)&?(.*?)$
RewriteRule ^ %{REQUEST_URI}?%1&%3 [CO=lang:%2:127.0.0.1:1:/:0:1,R,L]
#Remove potential prefix or suffix & from query string
RewriteCond %{QUERY_STRING} ^&(.*?)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&$
RewriteRule ^ %{REQUEST_URI}?%1 [R,L]
#External requests with \.php should be without that.
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#Try to load php page if resource does not alreay have an extension
RewriteCond %{REQUEST_URI} !\.[a-z]+$
RewriteRule ^(.*)$ $1.php [L]

How to retrieve data with htaccess Rewrite rule

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]

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