Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite doubt.

         

JoaoJose

3:25 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Hi there.

I'm trying do rename some files with htaccess.
After looking at some examples I got to this:

RewriteEngine On
RewriteRule ^something([0-9]*).html somethingelse.html?id=$1 [L]

So what I want is www.mydomain.com/something123.html

To get www.mydomain.com/somethingelse.html?id=123

This must be very simple but I canīt figure it out...

Thanks.

JoaoJose

6:13 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Solved :)

the code was alright just a mistake pasting it to the htaccess file...

jd01

2:43 am on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi JoaoJose

RewriteEngine On
RewriteRule ^something([0-9]*).html somethingelse.html?id=$1 [L]

In the .htaccess file your right side rule should begin with / because this is sent back through the server and processed from the root directory. (Another note is the right side of the rule should always be the /full/path/tothe/file.php, even if it is in the same directory as the originating file.)

The left side of the rule should have special characters that should be checked for an exact match preceded by a \ In this case the .(dot) should be 'escaped' EG \. to prevent it from matching 'any character except the end of a line'

You also would probably be well served by putting a hard ending $ on the left side of the rule.

The new version would look like this:

RewriteEngine On
RewriteRule ^something([0-9]*)\.html$ /somethingelse.html?id=$1 [L]

Just technicalities =)

Justin

JoaoJose

6:48 am on Jun 22, 2005 (gmt 0)

10+ Year Member



Thks :)

JoaoJose

1:18 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Well another doubt arises after the mod_rewrite.

I want to redirect all the old dynamic links I have to the new static pages.

The code I used to rewrite de urls was this and it works fine:

RewriteEngine On
RewriteRule ^blue-widgets-([0-9]*)\.html$ /page.html?blueid=$1 [L]
RewriteRule ^green-widgets-([0-9]*)\.html$ /page.html?greeid=$1 [L]
RewriteRule ^red-widgets-([0-9]*)\.html$ /page.html?redid=$1 [L]

After some research mainly on webmasterworld I came to something like this to redirect the old links:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.html\?blueid=([0-9]+)\ HTTP/

I think this first line is ok but what I donīt understand is the ([0-9]+), why the + and not *?

The second line I have no clues how to do it... why can't it be equal to the first rules I've used?

I've used an example on another thread and came to this:


RewriteRule ^page\.html\?blueid$ /blue-widgets-%1.html? [R=301,L]

is the %1 supposed to get the value out of ([0-9]+)?

also my idea was to repeat this line for blue, green and red widgets.

Hope someone can help me :)

JoaoJose

3:39 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Don't know why but the edit button is missing so I'll have to reply again.

After finding this very nice post [webmasterworld.com...]

I managed to clear some of my doubts so this is what I have:

#rewrites the dynamic urls and works fine

RewriteEngine On
RewriteRule ^blue-widgets-([0-9]+)\.html$ /page.html?blueid=$1 [L]
RewriteRule ^red-widgets-([0-9]+)\.html$ /page.html?redid=$1 [L]
RewriteRule ^green-widgets-([0-9]+)\.html$ /page.html?greenid=$1 [L]

#this would be repeated for blue,red and green widgets just like the above....but it doesn't work...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.html\?blueid=([0-9]+)\ HTTP/
RewriteRule ^/page\.html$ /blue-widgets-%1.html? [R=301,L]

Any ideas? Thanks in advance :)