Failed to use Apache RewriteRule - apache

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

Related

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]

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]

htaccess rewrite

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]

Apache rewrite rule with parameters?

I have the following URL:
http://domain.com/index.php?m=feedback&cSubject=My Subject
I want to have a rewrite rule so that the following:
http://domain.com/feedback?Subject=My Subject
maps to the previous url. Heres my rule at the moment:
RewriteRule ^feedback?Subject=(.*)$ index.php?m=feedback&cSubject=$1
Doesn't seem to be working tho! Any ideas?
Query Strings are not parsed by Apache Mod_Rewrite, but there is a workaround. Try this
RewriteRule ^feedback/?$ index.php?m=feedback&c%{QUERY_STRING} [NC,L]
You can use RewriteCond statement to do exactly what you want:
RewriteEngine On
RewriteCond %{QUERY_STRING} Subject=(.*)
RewriteRule ^feedback$ index.php?m=feedback&cSubject=%1 [L]
There seems to be an = missing from clops answer to give..
RewriteRule ^feedback/?$ index.php?m=feedback&c=%{QUERY_STRING} [NC,L]
.. at least I need one to make it work.