Apache redirect query string - apache

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]

Related

htaccess rewrite to remove query string

I need to rewrite the following:
http://www.mystuff.com/drinks/category/beer?page=1
to
https://www.mystuff.com/food-drink/beer/ale
No matter what I try the URI rewrite to the new address but it keeps the query string attached. I need to lose this. I've tried so many options and none seem to work, can anybody ofgfer some advice.
I thought this would do it, but no:
RewriteCond %{QUERY_STRING} (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale [R=301, L]
Can anybody help?
You need to add an empty query string to truncate it on the rewrite. Add a single ? to the end of the rewrite:
RewriteCond %{QUERY_STRING} (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale? [R=301, L]

Rewriting URL containing a '?'

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

Apache rewriterule with regular expressions

I am trying to rewrite multiple pages like this ...
http://subdomain.domain.com/index.php?action=printpage;topic=12345.67
To this ...
http://subdomain.domain.com/index.php/topic,12345.67.html
I have unsuccessfully tried to use ...
RewriteRule ^index\.php\?action=printpage;topic=([0-9]+)\.([0-9]+)$ http://subdomain.domain.com/index.php/topic,$1.$2.html [R=302]
Apache server and my other non-related rewrites work fine. Can anyone offer any suggestions? Thanks.
You cannot match against the query string in a RewriteRule, you need to use a RewriteCond and the % back reference:
RewriteCond %{QUERY_STRING} ^action=printpage;topic=([0-9]+)\.([0-9]+)$
RewriteRule ^index\.php$ http://subdomain.domain.com/index.php/topic,%1.%2.html? [R=302]

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]