Forum Moderators: phranque

Message Too Old, No Replies

mod-rewrite help

         

benkyma

3:22 pm on May 20, 2010 (gmt 0)

10+ Year Member



Hi,

I'm struggling with mod_rewrite. I need it to rewrite:
http://www.example.com/C/V

to:

http://www.example.com/?category=C&video=V

So far I have these two rules:



RewriteRule ^([^/\.]+)/?$ /index.html?category=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.html?category=$1&video=$2 [L]


but it doesn't seem to work. I'm not receiving any parameters through in my js on the other side. It also behaves differently depending on whether there is a slash on the end or not. Does anyone have more experience with this than me? (everyone probably : ))

[edited by: jdMorgan at 3:24 pm (utc) on May 20, 2010]
[edit reason] example.com [/edit]

jdMorgan

3:29 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume you've got the requisite directives to enable mod_rewrite and turn on the rewriting engine. If so, your code is fine.

But your JavaScript is client-side, while this mod_rewrite code implements client-requested-URL-to-internal-server-filepath rewrites. Therefore, your JS will 'see' only the "http://www.example.com/C/V"-format URLs.

Basically, you need to modify the JavaScript to look at the URLs in the form that they are published on your pages, not look for server-internal filepaths that the client doesn't even know about.

Jim

benkyma

3:34 pm on May 20, 2010 (gmt 0)

10+ Year Member



Of course, how can I get javascript to see the correct values then? Is it even possible?

jdMorgan

4:10 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes and no.

JavaScript will never ever see /index.html?category=cats&video=vids, but it will see http://example.com/cats/vids

So change the JS code to handle that, using the same kind of regular expression you're currently using on the server side.

None of this can ever be clear until you separate the client- and server-side concepts, and the URL and filepath concepts in your head. Browsers and JavaScript work only with URLs on the client side, and servers receive URL requests from clients and translate them to filesystem requests to serve content.

URLs and filepaths are two completely-separate things, and not necessarily related; URLs are only 'associated' with files by the action of a server -- and this URL-to-filepath association can be modified using mod_rewrite, as you have done...

You have changed your URLs to be "search friendly." You have added a rewriterule to "map" requests for these new search-friendly URLs to the script filepath on your server (which the old dynamic URL-format used to resemble, but now no longer does). Now you must also change all client-side code to use the new search-friendly URLs for all functions.

There's also a final step that you may wish to take after getting the rest of this working: Externally redirect all requests for the old dynamic-format URLs to the new search-friendly URLs. However, this must be the final step, as it can complicate testing all of the others.

Jim

benkyma

9:23 am on May 21, 2010 (gmt 0)

10+ Year Member



Ok, thanks a lot. I've got everything I need to get this working : )