Forum Moderators: phranque

Message Too Old, No Replies

mod rewriting a strange url with multiple commas

         

andyred

10:30 pm on May 19, 2010 (gmt 0)

10+ Year Member



Hi guys,

I'm having a big headache with this url /test12,name,100051,name_id,name_details

I would like to rewrite it to: test12-100051 and I'm having a lot of trouble doing it

I was thinking of something like:
RewriteRule ^(.*)-(.*)$ /$1,name,$2,name_id,name_details [R=301,L]
but it doesn't seem to work.

Any help will be greatly appreciated.

Thanks in advance

g1smd

10:40 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You asked for a rewrite, then provided code for a redirect.

Which one do you want? Are you sure of the difference between those two things?

Never use
(.*)
twice in a pattern. It is so inefficient, it will slow the processing by a factor of hundreds or thousands.

Use a much more strict pattern such as
([^\-]+)
and
([^/.]+)
for example.

Link to the URL format that you want people to see and use. URLs are defined in links. A rewrite cannot 'change' a URL.

The rewrite will connect the incoming URL request to the internal filepath.

There's example code in [webmasterworld.com...] and [webmasterworld.com...] and many other threads.

Additionally, a redirect will intercept requests for the old URL and tell the browser to make a new request for the new URL.

[edited by: g1smd at 10:49 pm (utc) on May 19, 2010]

andyred

10:44 pm on May 19, 2010 (gmt 0)

10+ Year Member



I want people to see test12-100051 not /test12,name,100051,name_id,name_details
From what I've read this can be accomplished only through a redirect.
Can you please show me what I'm doing wrong

andyred

11:01 pm on May 19, 2010 (gmt 0)

10+ Year Member



The examples you have gave are typical dynamic urls with ?var=value. For a beginner like me there's a big difference between typical dynamic urls and the one posted above with several commas

andyred

11:12 pm on May 19, 2010 (gmt 0)

10+ Year Member



Anyone? Jim?

g1smd

11:36 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So, adapt the example code and then test it on your server using Live HTTP Headers.

Be sure you know when you are dealing with a URL and when you are dealing with an internal filepath. They are two different things.

If there are problems with your code that you can't resolve, post your code here for discussion.

jdMorgan

3:37 am on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I want people to see test12-100051 not /test12,name,100051,name_id,name_details
> From what I've read this can be accomplished only through a redirect.

What you've read is wrong, then.

If you want users to see http://www.example.com/test12-100051, then that is what you must link to in the HTML on your pages.

When a client request for the URL-path /test12-100051 arrives at your server as the result of a user clicking that link on your page, rewrite it to the physically-existing file located at /test12,name,100051,name_id,name_details
 RewriteRule ^([^-]+)-([0-9]+)$ /$1,name,$2,name_id,name_details [L] 


I assume you've got a script at that location, although it seems an odd filepath...

I'm also assuming that "name", "name_id", and "name_details" are fixed literal strings. If they are not, then the whole plan will fail, because mod_rewrite will have no idea what the value of those variables should be, since the only information it has to work with is that contained in the string "test12-100051".

Anyway, the most important point is that already pointed out by g1smd: The link that you link to on your pages *is* the URL, and that URL will be seen and clicked by users, and followed and listed by search engines.

Jim