homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

How would you rewrite this?

 10:01 pm on Jul 6, 2011 (gmt 0)

Hi guys,

I have a weird URL i need to rewrite... I tried it all and its not working... any ideas on this:


to look like:


Thanks in advance!



 10:07 pm on Jul 6, 2011 (gmt 0)

You'll need a RewriteRule and you need the pages of your site to link TO the short URL because URLs are defined in links.

Let's see your code. It's a question that comes up several times per week and as such there are close on 5000 previous threads with example code.


 10:30 pm on Jul 6, 2011 (gmt 0)


Does the original search term really contain spaces? Oh, what a mess. To be safe you'd need to cover all possibilities because you have no idea what may have happened to the spaces in transit:
(%20|\ )?
(?:%20|\ )?
wherever the space occurs. The second version is if you're capturing other stuff in the area and you don't want to get mixed up.


 10:46 pm on Jul 6, 2011 (gmt 0)

lucy24, yeah, the spaces is what I'm not sure how to capture.

g1smd, I don't have it with me here, but I can post it later, it's just standard rewrites that for some reason dont work here.



 2:44 am on Jul 8, 2011 (gmt 0)

I have:

RewriteRule /tag/^(.*)\.htm$ /index.php?route=home/find&keyword=$1 [NC]

It's not working, because I'm not sure how to replace the space in $1 with a hyphen... any ideas?



 5:15 am on Jul 8, 2011 (gmt 0)

RewriteRule /tag/^(.*)\.htm$ /index.php?route=home/find&keyword=$1 [NC]

What is the sequence
intended to mean? As written, in non-initial position it is meaningless and possibly illegal. (I am not prepared to try it in Apache. In the text editor it didn't spit up an error message, it just didn't do anything.) There is a recent post by g1smd addressing this very point, but considering how hard it was to find my own post about spaces, I'm not even going to try looking for it :)

It's not working, because I'm not sure how to replace the space in $1 with a hyphen... any ideas?

Replacing one space is trivial. Replacing an indefinite number is tricky. My first thought was

RewriteRule \  - [N]

That's: one literal space character, escaped, followed by an un-escaped space for transition from input to output. But you can't do that, because the hyphen alone has special meaning in the replacement string. You'd have to escape it to \- to output a literal hyphen-- and you're not supposed to escape in the replacement string.

Better try it this way:

RewriteRule ([a-z])\ ([a-z]) $1-$2 [N]

For maximum protection I've enclosed the space in letters on both sides. (Add \d if necessary.) USE [N] WITH EXTREME CAUTION. It means "go back to the beginning as many times as necessary".

:: detour here to find near-identical post from within the last two or three days ::

Here: [webmasterworld.com...]

(###! Can't figure out how to link directly to post #4335955 near the bottom of the thread, dated July 6.)

As noted over there, if you need to do something with spaces, it is safest to make a separate rule for each possible number of spaces, assuming you'll never have more than nine (!) of them. For example

RewriteRule (my)(?:%20|\ )(search)(?:%20|\ )(term) $1-$2-$3 [NC]

Since your replace concerns the query, there will be some additional jiggery-pokery, but that's the basic principle. Note the (?: ) so you don't capture the spaces and get them mixed up with the parts you do want to keep. If the spaces have been genuinely obliterated in transit, rather than just encoded to %20, there's not much you can do unless there is a relatively small number of affected query strings that you can fix manually.

Once in a blue moon you'll meet something that somehow got double-encoded, like %2520 which reduces to %20. But this is all painful enough already.


 7:22 pm on Jul 8, 2011 (gmt 0)

Whoa, I'm lost... I'll read it again...

However, I think instead of having spaces, I can use a plus sign.

What if it was like this:




 8:19 pm on Jul 8, 2011 (gmt 0)

Plusses are not really optimal in Regular Expressions, because on the "pattern" side a plus has meaning so it always has to be escaped. But if you have the option of changing spaces in search strings before they ever reach .htaccess, definitely go for it. The safest is the lowline _ because RegEx treats it just like an alphanumeric; you can even pick it up with \w.


 8:12 pm on Jul 9, 2011 (gmt 0)

/ + - . and several other punctuation marks are legal in URL paths.

/ isn't supposed to be used in parameters.

Find the space?

^([^\ ]+)\ (.*)$ or similar.

Eliminate the space?


 12:22 am on Jul 10, 2011 (gmt 0)

/ + - . and several other punctuation marks are legal in URL paths.

In URL paths, as in real life, legal is not always the same things as a good idea ;)

And I have just realized that I have quite a large directory in which every single name at the top level occurs both as a filename and as a subdirectory name.

::slinking off in shame::

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved