Rewrite rule on URL query string - mod-rewrite

Can somebody help me how to replace query string URL. I want to redirect URL (my requirement to remove %2Fcontent%2Fabc from query string URL).
From :
http://something.com/en-US/login.html?resource=%2Fcontent%2Fabc%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
to:
http://something.com/en-US/login.html?resource=%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
I tried using rewrite condition and rule below but no luck.
RewriteCond %{QUERY_STRING} ^resource=%2Fcontent%2Fabc(.*)$
RewriteRule ^/(.*)?resource=%2Fcontent%2Fabc(.*)$ /$1?resource=%2 [L]

Related

ISAPI Rewrite : Append querystring to the rewrited URl's Query String

Actually, using the following ISAPI rule
RewriteCond %{HTTP:Host} ^domain.com$
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L]
I'm rewriting the following URL
htttp://www.domain.com/Product-name/
to
htttp://www.domain.com/test.cfm?ProducID=xxxx
This is working fine, But when I use the querystring in URL, it is not working
For Egs:
The following URL is not working
htttp://www.domain.com/Product-name?categoryID=YYYY
I need to rewrite the above URL as follows
htttp://www.domain.com/test.cfm?ProducID=xxxx&categoryID=YYYY
I've used the following rule, but no luck
RewriteCond %{QUERY_STRING} ^param=(\d+)$ [NC]
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx?param2=%1? [NC,L]
So is there any solution for this?
If you are using ISAPI_Rewrite 3, then you should add QSA flag to append original query string to the resulting URL:
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L,QSA]
The following rule, appends the query string to the rewritten URL's Querystring and avoids the 404 error
URL With Querystring and Trailing slash(/)
RewriteRule ^/Product-name/\? /test.cfm?ProductID=xxxx [NC,L,QSA]
URL With Querystring and Without Trailing slash(/)
RewriteRule ^/Product-name\? /test.cfm?ProductID=xxxx [NC,L,QSA]
URL Without Querystring and with Trailing slash(/)
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L]
URL Without Querystring and Trailing slash(/)
RewriteRule ^/Product-name$ /test.cfm?ProductID=xxxx [NC,L]

Rewrite URL so that query string is in path

I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha
To rewrite to this: /faculty/FirstLast
Am I correct to assume the following would be acceptable to put in .htaccess?
# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]
I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.
Outcome:
My .htaccess looks like
# Rewrite old URLS
RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
RewriteRule ^faculty/ /faculty/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} vidID=([0-9]*)
RewriteRule ^videos/ /video/id/%1/? [R=301,L]
I also found this wonderful tool -- a mod_rewrite tester
http://htaccess.madewithlove.be/
All good!
Try this instead:
RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]
The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.
As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.

RedirectMatch not working

I have this URL:
http://www.example.com/prospective/deginfo.php?classname=PE&diploma_description=BSc+in+Mathematical+Sciences
That I need to redirect to this URL instead:
http://www.example.ie/prospective/deginfo.php?classname=PEC&diploma_description=BSc+in+Mathematical+Sciences+with+French+%27Bachelor+Honours+Degree%27
I thought I could do the following with a RedirectMatch to "catch" everything up to Bsc and from there get it to redirect to the URL I want but it is not working :
RedirectMatch ^/.*BSc\+in\+Physical\+Education http://www.example.ie/prospective/deginfo.php?classname=PEC&degree_description=BSc+in+Mathematical+Sciences+with+French+%27Bachelor+Honours+Degree%27
Can anyone advise what I'm doing wrong and how to correct please?
You can't match against the query string in a RedirectMatch, it only matches against the URI. You can match against it using mod_rewrite and a RewriteCond:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^classname=PE&diploma_description=BSc\+in\+Mathematical\+Sciences$
RewriteRule ^/?prospective/deginfo.php$ /prospective/deginfo.php?classname=PEC&degree_description=BSc+in+Mathematical+Sciences+with+French+'Bachelor+Honours+Degree' [L,R]

.htaccess $_GET['p'] not working

With this url http://www.example.com/cp/pending/thepage, when i $_GET['p']; i should get thepage returned. Nothing is being returned, How do i solve this?
Here is the mod rewrite.
RewriteRule ^cp/([A-Za-z0-9-]+)(&type=[A-Za-z0-9-]+)?(/[A-Za-z0-9-]+)?(&to=[A-Za-z0-9-]+)?(&r=[A-Za-z0-9-]+)?(&g=[A-Za-z0-9-]+)?(&page=[A-Za-z0-9-]+)?/?$ /cp.php?o=$1&type=$2&c=$3&p=$4&r=$5&g=$6&page=$7 [L]
Here is php
$p = $_GET['p'];
echo $p;
The query string is not part of the URL.
Your code should be like the following:
RewriteCond %{QUERY_STRING} RegexHere
RewriteRule URLRegexHere NewURL
If you use any part of the URL to create a new query string, you need to use the [QSA] flag to append the other parameters. E.g:
RewriteRule ^cp/pending/([^/])/?$ page.php?page=$1 [L,QSA]
More on Mod_Rewrite here.
Also, just as $n back-references are for patterns matched in the RewriteRule regex, %n back-references are for pattern matched in the RewriteCond regex.

mod_rewrite RewriteRule pattern match fails when query string is URL-encoded

I am trying to use mod_rewrite RewriteRule, and in my RewriteRule, I am trying to match a URL that has query string that looks like:
http:///myfakeoam/obrareq.cgi?....
My RewriteRule looks like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^wh=(.*)$ [NC]
RewriteRule ^/myfakeoam/obrareq.cgi$ http://apache1.whatever.com/formbasicprotected/index.html [CO=wh:%1:.whatever.com:1440:/]
When I test manually, by manually typing the URL into the browser, that RewriteRule seems to be able match a request if the request looks like:
http:///myfakeoam/obrareq.cgi?wh=xxx&ru=yyyy&....
but, in my actual system, the request is being created by anoher app, and it appears to be URL-encoding (actually URL-encoding twice) the query string (e.g, replacing equal with "%3D", etc.), i.e.:
http:///myfakeoam/obrareq.cgi?wh%3Dxxx....
And if the query string part is URL-encoded like that the pattern match in my RewriteRule is not failing...
Is there any way to handle this situation?
Thanks,
Jim
Answering my own question, I just realized that the RewriteCond was just doing a regex match for the query string, so I changed that to:
RewriteCond %{QUERY_STRING} ^wh%3D(.*)$ [NC]
and then it worked.
Jim

Resources