Replace %22 by " with mod_rewrite in query string - mod-rewrite

Have tried a lot of RewriteRule, even the previous suggested posts. Unfortunately, none works. Any help appreciated.

This will replace all %22 with " in the query string:
RewriteCond %{QUERY_STRING} ^(.*)\%22(.*)$
RewriteRule ^(.*)$ /$1?%1"%2 [L,R]
However, the & is used to separate query string parameters, so if you start with:
?abc=%22def%22&xyz=123
It'll change it to:
?abc="def"&xyz=123
which means the parameters will be
abc=
quot;def
quot;
xyz=123
You probably want to escape the & so that doesn't happen:
RewriteCond %{QUERY_STRING} ^(.*)\%22(.*)$
RewriteRule ^(.*)$ /$1?%1\%26quot;%2 [L,R,NE]

Related

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

.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.

RewriteCond Simple Issue

I need the URl http://mydomain.com/careers to go to http://mydomain.com/#!/careers
For what it's worth I've tried numerours variations around this with no success
RewriteCond %{REQUEST_URI} !^/careers$
RewriteRule (.*) /#!/careers [QSA,L]
Can anyone help?
"!" in your RewriteCond line means "not"...
Also, "[QSA,L]" means:
L means this is last rule (processing terminates after matching this one) and
QSA means query string append
But, becase R flag was not specified, this is done by sub-request and not a redirect, so the actual URL in your browser does not change...
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/careers$
RewriteRule (.*) /#!/careers [R,L,QSA]
Hope this helps

Replace ? in request with RewriteRule

site.com/link?p=2
give $_GET['p']==2 even though i've already made
site.com/link
rewrite to
site.com/index.php?page=link
So, i'm trying to replace site.com/link?p=2
with site.com/link&p=2
RewriteEngine on
RewriteRule (.*)\?(.*) $1\&$2
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1
It doesn't work!
RewriteRule cannot see query strings (the ? and anything after it) on the left-hand side; it matches only on the path part of the URL.
But the good news is, all you probably need to do is this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1 [QSA]
The QSA option, Query String Add, tells your RewriteRule to add to the query string instead of replacing it (the default behavior, which doubtless prompted the whole issue).

help rewriting this url. simple but not working

RewriteCond %{REQUEST_URI} !^/?ping.php
RewriteRule ^/\?(.*)$ ping.php?url=$1 [L]
i am trying to match any character that follows /?
www.mysite.com/?someurl.com
instead it keeps loading the index.html !
You cannot match the query string with mod_rewrite, but if you still want to pass it on, you can add %{QUERY_STRING} to the resultant url, for example
RewriteRule ^.*$ ping.php?url=%{QUERY_STRING} [L]

Resources