Handling parameter values containing % character in a rewrite rule in apache - mod-rewrite

I use following RewriteRule syntax in apache and IBM HTTP servers:
RewriteRule ^/target$ www.something.com/content?param1=value1&paramN=valueN [R=301,L,NC,NE]
If a value contains % character, it does not work.
I tried dropping NE flag, no luck.
I tried encoding the % character itself as %25, no luck.
When the rewritten URL shows up on the browser, the % character is missing.
Can someone help with the correct RewriteRule syntax with an example?
Thank you very much for any help

Prefixing the % character with a backslash \ solved the problem.
This syntax corrupts the value of parameter p1 by dropping the % character:
RewriteRule ^/target$ www.something.com/content?p1=abx%xyc [R=301,L,NC,NE]
Prefixing % with a backslash preserves the value of parameter p1:
RewriteRule ^/target$ www.something.com/content?p1=abx\%xyc [R=301,L,NC,NE]
The % character causes the next two characters to be treated as the hex value of an ASCII character. This substitution alters the value of the parameter in the destination URL. Using the \ prefix prevents this substitution.

Related

URL rewrite rule to remove trailing slash after query string

we have people coming to the website with trailing slash at the end of the URL e.g https//www.domain.com/page?name=john&name=doe/
for some reason, the logic to read query string parameters in code fails if there is a trailing slash at the end of the query string. Is there any way I can write a rule to check if there is trailing slash at the end of the query string them remove it.?
RedirectRule ^(http.+?\.com\/.+?\?.+?)\/$ $1
This should work, but not sure what rewrite regex format you need on iirf before entering to your page. I am using iirf.ini on my aspx C# project.
^...$ - asserts position at start and end of a line
.+? - match any char with quantifier one and unlimited, lazy
\. \/ \? - escape character for regular expressions
$1 - group 1 from the regex first bracket

Allow '&' sign in URL (mod rewrite)

Is there anyway I can allow the "&" sign as a variable in the URL?
My current rule for tags is:
RewriteRule ^tag/([^/.]+)/?$ index.php?section=home&tag=$1 [L,QSA,NC]
Tags like "cool" and "nice45" work. But "this&that" doesn't work.
Any suggestions?
Thanks.
Use both [B] and [NE] flags :
RewriteRule ^tag/([^/.]+)/?$ index.php?section=home&tag=$1 [L,QSA,NC,B,NE]
[B] will escape & before applying the transformation,
then it won't be caught as tag=this and that=
[NE] will avoid & to be escaped a second time after the transformation
> mod-rewrite flags documentation

accept parentheses as a character in mod_rewrite

i have to redirect some old page to new page i used below commend in .htaccess
RewriteRule ^Savane-98.htm$ savane-tc-9347-41-belgian-tapestry-throw [R=301,L]
it works fine but some of my old link have parentheses '()' it don't work how can i solve that . like below link
RewriteRule ^antique-bronze-square-end-rod-(flat-to-wall)-tapestry-wall-hanging-rod$ antique-bronze-square-end-rod-flat-to-wall-tapestry-rod [R=301,L]
this link don't work can anyone help me
Thanks
Parentheses are special characters in regular expressions - they denote the beginning and end of a matching group.
In order for them to be treated as literal parentheses by the regular expression engine, they need to be escaped with a backslash (\):
RewriteRule ^antique-bronze-square-end-rod-\(flat-to-wall\)-tap...

mod_rewrite is being too strict

I have a rewrite rule that is too strict.
RewriteRule ^folder/([^/\.]+)/?$ file.php?$1 [L]
This redirects http://www.domain.com/folder/$variable to http://www.domain.com/file.php?$variable HOWEVER.. if the variable has any dots or slashes it fails. I want it to redirect to http://www.domain.com/file.php? even if $variable is empty or contains any characters
Change ([^/\.]+) to (.*?).
The first matches at least one of anything except dots and forward slashes. The second matches 0 or more of anything, up to the posibility of a trailing slash (because of the /?$ at the end of your regex).

Redirect Calls for specific File Extension

I am trying to redirect any calls to a specific file extension (here .jphp) to another url, using the filename part of the call as a subfolder. I try:
RewriteEngine On
RewriteRule [^/]/([^\.jphp])\.jphp /js/$1/
It should ignore anything before the last "/", then store the Filename in $1 and redirect to HOST/js//
What am i doing wrong?
A ^ as the first character in a regex character class [] is an inversion. [^/] evaluates to "any character that is NOT a slash", and the [^\.jphp] evalutes to "any charater that is NOT a ., j, p or h.
Since you're not specifying charater repetition limits, you're limiting your urls to be of the form
x/z.jphp
where x and z are any single characters which are NOT one of the excluded characters mentioned above.

Resources