Allow '&' sign in URL (mod rewrite) - 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

Related

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

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.

Replace html encoding with url encoding

We are getting random hits like:
/abc?p=2&utm_campaign=xyz-campaign&utm_medium=email&utm_source=newsletter
Notice & which is html encoding for &. I have checked the possible sources but they all contain '&' only.
I want replace all & with &. Is there a way to achieve it? I have removed complete string as of now using below given rule. But this is not right!
RewriteCond %{QUERY_STRING} (&)
RewriteRule (.*?) https://%{HTTP_HOST}%{REQUEST_URI}? [R=302,L]
Capture the referer in your logs so you can find the source of the problem and (also) fix it there.
RewriteCond %{THE_REQUEST} ^\S++\s++([^\s?]*+\?\S*?&)amp;(\S*+)
RewriteRule ^ %1%2 [DPI,L,R=301]
That will do one redirect per ampersand without messing about decoding and re-encoding the URL. Less efficient but more reliable.

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

Trying to write a mod_rewrite rule

I've changed my CMS and need to write a mod_rewrite rule to help redirect some of the old URLs.
What I'd like to do is:
remove "blog/archives"
replace underscores with dashes
replace ".html" with a trailing slash
The old link:
http://example.com/blog/archives/the_post_title.html
The new link
http://example.com/the-post-title/
To address 1 & 3 I thought something along the lines of this might work, but it's not.
RewriteRule ^/blog/archives/([A-Za-z0-9-]+)/?.html$ $1 [L]
Thanks for your suggestions.
For 1 and 3
RewriteRule ^/blog/archives/(.*?).html$ /$1/ [L,R=permanent]
(note that R=permanent use a 301 redirect, which will be cached for long time but does move your pagerank to the new URL. Use [L,R] to use normal redirection)

Isapi Rewrite - access query string pattern matches in RewriteRule

I'm using Isapi Rewrite 3 (mod rewrite clone for IIS) and trying to rewrite URLs based on the query string - and then pass on part of that query string in the rewrite.
So if I enter a URL like this: /test/home.cfm?page=default&arbitraryExtraArg=123
I want that to be rewritten as: /test/index.cfm?page=home&arbitraryExtraArg=123
I have the following condition/rule:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [I,R=301]
But the extra query string variables are never passed. %1 seems to be blank.
This is how to reference a pattern match from the RewriteCond, right?
What am I missing here?
Thanks!
EDIT: It looks to me like the RewriteCond is being totally ignored. Here is what my logfile looks like:
[snip] (2) init rewrite engine with requested uri /test/home.cfm?page=default
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/home.cfm'
[snip] (1) escaping /test/index.cfm?page=home
[snip] (2) explicitly forcing redirect with http://www.devsite.com/test/index.cfm?page=home
[snip] (2) internal redirect with /test/home.cfm?page=default [INTERNAL REDIRECT]
[snip] (2) init rewrite engine with requested uri /test/index.cfm?page=home
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/index.cfm'
Should there be mention of the RewriteCond pattern check in there?
But the extra query string variables are never passed. %1 seems to be blank.
%1 is blank because according to the log the request you make is /test/home.cfm?page=default - without second parameter.
The absense of RewriteCond processing in the log may be due to low RewriteLogLevel.
And the config should be:
RewriteCond %{QUERY_STRING} ^page=default(.+)$ [NC]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [NC,R=301,L]
I think ISAPI Rewrite is a little different to mod_rewrite:
Whenever you put parentheses in any regular expression present in a complex rule (a rule with conditions) you are marking a submatch that could be used in a format string (using $N syntax) or as a back-reference (using \N syntax) in subsequent conditions. These submathces are global for the whole complex rule (RewriteRule directive and corresponding RewriteCond directives). Submatches are numbered from up to down and from left to right beginning from the first RewriteCond directive (if such directive exists) corresponding to the RewriteRule.
So try $1 to get the match of the first group no matter where it appears:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home\.cfm$ /test/index.cfm?page=home$1 [I,R=301]
ISAPI Rewrite 3 seems to be more compatible to Apache’s mod_rewrite.
I believe this works also:
RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]

Resources