Rewriting URL containing a '?' - apache

I have a rewriting rule which transform :
www.test.com/123456789
to
www.test.com/task.php?name=123456789
Now I am looking to do the same with a question mark before :
www.test.com/?123456789
I've tried :
RewriteRule ^\?([0-9]{10})$ task.php?name=$1 [L,NC,QSA]
But it does not work : no erros, but not redirected to task.php
I think that the '?' may cause issue..
Thanks for help.

You can use that in your .htaccess for www.test.com/?123456789:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\d+)$
RewriteRule ^$ task.php?name=%1 [L]
And by the way... 123456789 -> [0-9]{9} and not {10}
If 10 is important you can use that:
RewriteCond %{QUERY_STRING} ^(\d{10})$

Try using
RewriteRule ^\?([0-9]{10})$ task.php?name=$1 [L,NC,QSA]
To capture your number and pass it into the new URL

Related

Modify query string value using htacces

I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]

Apache redirect query string

I'm new in building reverse proxy apache.
I've a query string like this:
host.example.com/some/thing/?company=string with spaces l.t.d.
And i want it to be seen like:
host.example.com/some/thing/string with spaces l.t.d.
I've written this:
RewriteCond %{QUERY_STRING]} ^company=(.*)$
RewriteRule ^/some/thing$ /some/thing/%1 [NC,L,R=301]
But both the RewriteCond and RewriteRule don't work.
Any help it's really appreciate.
Thanks
You are actually not matching against the %{QUERY_STRING} variable, remove the ] from the variable .
Try :
RewriteCond %{QUERY_STRING} ^company=(.*)$
RewriteRule ^/?some/thing$ /some/thing/%1? [NC,L,R=301]

RewriteRule : getting one parameter without QSA

I have this url:
example.com/dolce-vita/fr/camping.php?CAMPING-LES-MYRTILLES&p=839000231
And i want it to be redirected to this one:
example.com/rechercher-sur-le-site-45-1.html?q=CAMPING-LES-MYRTILLES
I tried this:
RewriteRule camping\.php?(.*) /rechercher-sur-le-site-45-1.html?q=$1 [NE,R=301,L]
RewriteRule camping\.php\?([a-zA-Z0-9]+)& /rechercher-sur-le-site-45-1.html?q=$1 [NE,R=301,L]
I'm a bit lost as i can't use QSA flag : in the url i want to rewrite, i have p=XX wich is a parameter i already use (ex : /rechercher-sur-le-site-45-1.html?q= means index.php?p=45) so if i use QSA, it adds p=839000231 at the end and overwrites my p=45 parameter...
Tanks a lot for your help :)
You can do it this way
RewriteCond %{QUERY_STRING} ^([^&]+)
RewriteRule ^dolce-vita/fr/camping\.php$ /rechercher-sur-le-site-45-1.html?q=%1 [NE,R=301,L]
More info about %{QUERY_STRING} here
More info about mod_rewrite here

Failed to use Apache RewriteRule

I would like to use Apache RewriteRule to change the URL target page to abc.php. I have set RewriteEngine On but I found this problem.
Regexp I used:
RewriteRule ^viewthread\.php.tid=12345$ abc.php
The URL string to match:
viewthread.php?tid=12345
Why it is not successfully matched?
Rewriting URLs with query strings is slightly more complicated than rewriting plain URLs. You'll have to write something like this:
RewriteCond %{REQUEST_URI} ^/viewthread\.php$
RewriteCond %{QUERY_STRING} ^tid=12345$
RewriteRule ^(.*)$ http://mydomain.site/abc.php [R=302,L]
See those articles for more help:
http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/
http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/
i think because you have missed the ? in the rule...
RewriteRule ^viewthread.php?tid=12345$ abc.php
Shouldn't it be:
RewriteRule ^/viewthread\.php\?tid=12345$ /abc.php

Getting URL parameters with mod_rewrite

Here is my current rewrite rule
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^post/([^/\.]+)/?$ index.php?post=$1 [L]
RewriteRule ^cat/([^/\.]+)/?$ index.php?cat=$1 [L]
three lines to do basically the same thing. Is there a way I can clean this up a bit? Is there one line that will accomplish this?
Also, Is there a way to pull multiple variables from a single url string? So if I had something like:
http://www.mysite.com?page=foo&id=123&color=red
how would I convert it to this:
http://www.mysite.com/page/foo/id/123/color/red
RewriteRule ^(cat|post|page)/([^/\.]+)/?$ index.php?$1=$2 [L]
because jacek_K to you one part, I give you answer of second part :
to rewrite this URL(http://www.mysite.com/page/foo/id/123/color/red), use this:
RewriteRule ^page\/([^/\.]+)\/id\/(\d+)\/color\/([^/\.]+) index.php?page=$1&id=$2&color=$3 [L]