Solved

mod url rewrite cannot decode + sign

Posted on 2009-06-29
9
1,388 Views
Last Modified: 2012-06-27
I have used htaccess to di my redirecting of pages but i am unable to pass a parameter with '+' sign in it

i have tried
RewriteRule ^articles/(.+)/([0-9]+)/$ category.php?category_name=$1&pageNo=$2 [NE]

this is what i typed in my url -> http://www.mywebsite.com/articles/Food+Drink/1/
i did an echo on the parameter category_name on the redirected page and it reflects Food Drink, the + sign is missing

please kindly assist me on this
0
Comment
Question by:ginola29
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:lharrispv
ID: 24740223
This is not exactly what you are asking but you might be able to gleam some info from it anyway.

http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_23647266.html
0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24740235
Here is another one for you.  It comes from:http://www.webmasterworld.com/apache/3890963.htm

Neither problem can be fixed simply by modifying the regex.
in the first case, the code is specifically "skipping" the rewrite if a period or slash is found in the requested (pretty) url and no colon precedes those characters (whether immediately-preceding or not). The intent is apparently to avoid rewriting urls that have filetypes appended, such as "robots.txt" or "index.php" itself.

in the second case, it is likely the script itself changing plus signs to spaces, as this is a common thing, for example among most search engines.

So the first question to ask is, "Can you do without periods and plus signs in titles?" Noting that these are titles --such as used in newspapers-- periods are never used, and I doubt that plus signs are ever used, either.

If the answer is "No," then you'll probably have to go with a rewriting solution that is a lot less efficient. And by that I mean "very inefficient" and may not be suitable for a high-traffic site. You will also have to modify the Wiki script to fix the plus sign problem, as that can't be fixed in mod_rewrite.

Modification for "period" problem:

# Rewrite url-path requests with no periods or slashes (unless preceded by a colon) to script
RewriteRule ^([^./]+¦[^:]*:[^./]*[./].*)$ /index.php?title=$1 [QSA,L]
#
# Else rewrite only if requested url-path does not end with any of the listed
# filetypes and does not resolve to a physically-existing directory or file
RewriteCond $1 !\.(gif¦jpe?g¦png¦php[345]?¦s?html?¦xml¦js¦css¦txt¦rdf¦pdf¦flv¦swf¦wmv¦mpe?g[234]?¦avi¦mp3)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?title=$1 [QSA,L]

It is the additional second rule and how it works that is inefficient: Any requested url-path with a period or slash which is not preceded by a colon and that does not end with the specifically-listed filetypes will result in two calls to the operating system's file manager --and possibly in two disk reads-- to go see if the requested url-path resolves to an actually-existing physical file or directory. These extra disk functions may slow the site perceptibly if you get a *lot* of traffic, meaning hundreds of thousands of hits per day. You can reduce the number of checking operations by specifically excluding certain filetypes from being checked as shown, but note that any "filetype" that you exclude here cannot be used at the end of a 'title' in your Wiki. The list shown here is meant to be illustrative, rather than comprehensive: add or remove filetypes to suit your site's needs. Filetypes appearing in the list cannot appear at the end of Wiki titles, but won't result in a disk check. You must decide where to strike the 'balance' based on what is important to you.

Also, order the filetype-exclusion list based on your 'hit-count' stats, with the most frequently-requested filetypes at the beginning of the list.

Again it's a balance between executing more mod_rewrite code, titling freedom, and disk-check time.

Important: Replace the broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe characters, and the code won't work if the broken pipes are not corrected.

And again, the problem with plus signs is not resolvable by mod_rewrite -- I'd suggest you either avoid the use of that character, or use preg_replace in a "patch" in the script to substitute either a different character or the word "plus" for all occurrences of that character in your title links (look for the "link printing" or "get title from database" directives, and add the preg_replace in or near those lines).

I should also say that I don't know why the original code looked for colons -- It may have something to do with the urls produced by the Wiki script; Perhaps you can include periods and slashes in Wiki titles if you precede them with a colon (check the documentation, as you may not even need to modify this code). But I attempted to copy that functional behaviour into the new code above, and will leave that issue for you to investigate.

0
 
LVL 4

Expert Comment

by:TurboBorland
ID: 24740248
Because it's considered an illegal character as it is used for things outside of just the symbol.  Did you try to convert the + sign to %2B and trying that out?
0
 

Author Comment

by:ginola29
ID: 24740259
i've tried using php's rawurlencode and converted it to %2B, but it still doesn't work
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Expert Comment

by:jasonsbytes
ID: 24740560
You really should avoid using + sign in URL's.  There is much documentation about this being a bad idea.

Can you use a different character?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24741041
Using the B flag should fix your issue. The B flag will work from version 2.2.12 (not released yet). There's a bug in versions below 2.2.12. Therefore you'll have to recompile mod_rewrite with the latest http://svn.apache.org/repos/asf/httpd/httpd/branches/2.2.x/modules/mappers/mod_rewrite.c
0
 

Author Comment

by:ginola29
ID: 24742651
hi caterham, i do not have access to my web server, is there any other solutions?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24743597
You could convert a space into a + in php, e.g. with ereg_replace or preg_replace.
$result = preg_replace('/ /', '+', $input);

Open in new window

0
 

Accepted Solution

by:
ginola29 earned 0 total points
ID: 24748936
thanks guys for you assistance, but i think i found my solution to my question

this article solved my problem
http://www.mediawiki.org/wiki/Manual:Short_URL/Allowing_for_escaped_characters_in_URI
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now