Script to replace spaces with underscores in URLs

A place to discuss the science of computers and programs, from algorithms to computability.

Formal proofs preferred.

Moderators: phlip, Moderators General, Prelates

User avatar
Eebster the Great
Posts: 3484
Joined: Mon Nov 10, 2008 12:58 am UTC
Location: Cleveland, Ohio

Script to replace spaces with underscores in URLs

Postby Eebster the Great » Fri Feb 24, 2012 2:23 am UTC

So the two sites I visit the most often are Google and Wikipedia, and in my current arrangement I have Google as the default search in the search bar in Firefox (so I can use the calculator without leaving the page; location bar search doesn't allow that) and http://en.wikipedia.org/wiki/ as the default URL prefix for location bar misses. This allows me to type something like Computer_science into the location bar and immediately reach that page on Wikipedia. However, the underscore is not convenient to type, so I would rather just use the space bar. Unfortunately, Firefox replaces these with pluses by default, since it thinks I am searching for keywords (rather than putting in an exact title).

I'm sure it would be very simple to write a script that replaces spaces in the location bar with underscores, but I am not good with computer and have no idea how to do this. Can anyone help?

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Script to replace spaces with underscores in URLs

Postby phlip » Fri Feb 24, 2012 2:56 am UTC

Simpler solution: change the prefix to http://en.wikipedia.org/wiki/Special:Search/ - that way, not only do spaces get handled correctly, but if you type something that isn't an actual page name, it'll take you to the search results instead. However, that said, Wikipedia will take a URL like http://en.wikipedia.org/wiki/Foo%20Bar and redirect you to http://en.wikipedia.org/wiki/Foo_Bar. If your browser is encoding spaces as + instead of %20, then that doesn't work in the path, only in the query string... so you'll have to instead make the prefix http://en.wikipedia.org/wiki/Special:Search?search=

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Eebster the Great
Posts: 3484
Joined: Mon Nov 10, 2008 12:58 am UTC
Location: Cleveland, Ohio

Re: Script to replace spaces with underscores in URLs

Postby Eebster the Great » Fri Feb 24, 2012 3:27 am UTC

phlip wrote:Simpler solution: change the prefix to http://en.wikipedia.org/wiki/Special:Search/ - that way, not only do spaces get handled correctly, but if you type something that isn't an actual page name, it'll take you to the search results instead.

Oh I didn't realize that search brought you directly to pages. That still isn't a solution though, because of the problem below.

However, that said, Wikipedia will take a URL like http://en.wikipedia.org/wiki/Foo%20Bar and redirect you to http://en.wikipedia.org/wiki/Foo_Bar. If your browser is encoding spaces as + instead of %20, then that doesn't work in the path, only in the query string... so you'll have to instead make the prefix http://en.wikipedia.org/wiki/Special:Search?search=

The browser is changing spaces into pluses specifically for the location bar keyword search, presumably because it assumes I am searching for keywords. If I put in a valid URL first, the spaces are treated normally.

E: I just realized that doesn't make a lot of sense as stated.

Firefox first queries the DNS (I know this because my ISP used to give me this annoying search for every URL miss, which I had to disable), and then if it misses it switches to the keyword search. It's that search I was struggling with.

The PHP link seems to work nicely though, thanks.

User avatar
freakish777
Posts: 354
Joined: Wed Jul 13, 2011 2:14 pm UTC

Re: Script to replace spaces with underscores in URLs

Postby freakish777 » Fri Feb 24, 2012 3:43 pm UTC

If FF or Chrome is your main browser, a plugin or Add-On should already be created that does what you want:


https://addons.mozilla.org/en-US/firefo ... url-fixer/

I think this plugin does what you want, if not find a similar plugin that does.


Return to “Computer Science”

Who is online

Users browsing this forum: No registered users and 5 guests