Apache rewriterule with regular expressions - apache

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]

Related

Apache .htaccess rewrite parameter to directory

I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result

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]

apache rewrite rules

i'm trying to get the following url to be rewritten
http://blog.mywebsite.com:8080/wp-admin/users.php?paged=2
as
http://mywebsite.com/blog/wp-admin/users.php?paged=2
i have tried a variety of rewrite conditions and rules
RewriteCond %{QUERY_STRING} ^(.*)$ [NC]
RewriteRule blog.mywebiste.com(.*)$ mywebsite.com/blog/$1
i seem to loose the query string when id do this
RewriteRule ^/wp-admin(.*) http://mywebsite.com/blog/wp-admin$1
this doesn't seem to catch it at all
also i have the rewrite log on. When does this get written to?
It should work without leading forward-slash:
RewriteRule ^wp-admin/(.*) http://mywebsite.com/blog/wp-admin/$1

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

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.