RewriteMap not working for urls with query params - apache

It seems rewritemap is not considering query params in matching url. Please suggest a solution.
My setup in httpd.conf file:
RewriteMap redirects dbm=db:/usr/local/apache/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]
redirects file has urls lile
/same_url/ http://mysite.com/
/same_url/?q=1 http://mysite.com/q2=1
/same_url/?q=2 http://mysite.com/q2=2
/same_url/?q=3 http://mysite.com/q2=3
But all 4 urls are getting directed to http://mysite.com only. So it seems matching is only done against non-query part.
Please help.

Looking at your RewriteMap it seems that you do not need to use a map.
You can do it without a map:
RewriteCond %{QUERY_STRING} ^q\=([0-9]+)$
RewriteRule ^/same_url/(.*)$ http://example.com/q2=%1 [redirect=permanent,last]
Pay attention to the %1 in the RewriteRule backreferencing to the match in the RewriteCond.
If you still want to use a Rewrite map you might have to "transform" the query string into something in the path
The rules could be:
RewriteCond %{QUERY_STRING} ^q\=([0-9]+)$
RewriteRule ^/(.*)$ /$1/q=%1 <-- here the GET param is transformed to something in the path
RewriteMap redirects dbm=db:/usr/local/apache/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]
You'll have to change your map to something not considering GET params:
/same_url/ http://example.com/
/same_url/q=1 http://example.com/q2=1
/same_url/q=2 http://example.com/q2=2
/same_url/q=3 http://example.com/q2=3

Related

Rewrite Apache Query_String

I have a query string URL: http://localhost/cgi-bin/qgis_mapserv.fcgi?map=/home/qgis/project/map.qgs. I want to hind the map.qgs path in the variable MAP. Besides the map variable, there are some variables (version, request, service, etc).
Here is what I need:
RewriteRule:
Pattern: ^cgi-bin/qgis_mapserv.fcgi?map=map.qgs&(.)$
Substitution: ^cgi-bin/qgis_mapserv.fcgi?map=/home/qgis/project/map.qgs&(.)$
Find bellow my unsuccessful attempt:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgi-bin/qgis_mapserv.fcgi$
RewriteCond %{QUERY_STRING} ^map=([A-Za-z0-9.-_]+)$
RewriteRule ^cgi-bin/qgis_mapserv.fcgi?(.*)$ cgi-bin/qgis_mapserv.fcgi?$1
Note: The map variable can show up in the pattern uRL anywhere among the other variables.
I wonder what I am missing on the code above
You are almost there.
The reason why your rule isn't working is because you can't test queryString in pattern of a RewriteRule.
You need to change your rule's pattern to
RewriteRule ^cgi-bin/qgis_mapserv.fcgi$
With this change your htaccess rules will look like :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgi-bin/qgis_mapserv.fcgi$
RewriteCond %{QUERY_STRING} ^map=([A-Za-z0-9./-_]+)/map.qgs$
RewriteRule ^/?cgi-bin/qgis_mapserv.fcgi$ /cgi-bin/qgis_mapserv.fcgi?%1 [R,L]

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

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

Rewrite rule always redirect to wrong url

Hello All,
I need to rewrite http://mysite.com/user/profile/following?profile_name=MYNAME to http://mysite.com/user/profile/MYNAME/following
I have written rule like this:
RewriteRule user/profile/(.*)/(.*) /user/profile/$2?profile_name=$1 [L,R=301]
when i put url like http://mysite.com/user/profile/MYNAME/following in the browser it always redirect me to http://mysite.com/user/profile/following?profile_name=MYNAME
What did i miss?
Thanks in Advance
You can use this code:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+user/profile/([^?]+)\?profile_name=([^\s&]+) [NC]
RewriteRule ^ /user/profile/%1/%2? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^user/profile/([^/]+)/([^/]*)/?$ /user/profile/$2?profile_name=$1 [L,NC,QSA]

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.