Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Syntax

Need help with syntax

         

Clark

5:11 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I know that

RewriteRule ^p([0-9]+)-(.*).html$ script.php?p=$1 [L]

Will pass the data from this url:

[domain.com...]

to the following script as such:

[domain.com...]

But I'd like to do the same thing with text. Something like:

RewriteRule ^u([0-9]+).html$ username.php?u=$1 [L]

Where a call to [domain.com...]

would call
[domain.com...]

Is this possible? I assume that [0-9] means numbers only. So how would you make it pass only letters and numbers and no special characters?

jdMorgan

8:07 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add the letter range inside the [group], along with the number range, and add the [NC] flag to make the comparison case-insensitive:

RewriteRule ^u([a-z0-9]+)\.html$ /username.php?u=$1 [NC,L]

or alternatively,

RewriteRule ^u([A-Za-z0-9]+)\.html$ /username.php?u=$1 [L]

The first variant is more efficient.

I hope I understood your question...

Note that the periods in your patterns need to be escaped as shown.

See the references in our forum charter for a nice regular epressions tutorial.

Jim

Clark

1:45 pm on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Works like a charm! Many thanks!

JayDev

9:13 pm on Dec 18, 2004 (gmt 0)

10+ Year Member



I have the following issue with my .htaccess file

Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /mypage\.php?addition2=$1 [NC,L]
RewriteRule ^(.*)\.html$ /my-other-page\.php?addition3=$1 [NC,L]
RewriteRule ^index\.html$ /index\.php [NC,L]

It reads the first rewrite Rule but not the following ones. Any hint?

I also tried the same htaccess with [L] instead of [NC,L]

No success

jdMorgan

3:33 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JayDev,

You need to decide what you want your rules to do. Your first rule will always match anything the next two might match, so as a result, the first rule will always run for any ".html" page request, and the next two will never run.

The regular-expressions patterns in your three rules must be designed to detect the cases where you desire to rewrite to the three different URLs.

Jim

JayDev

7:48 pm on Dec 19, 2004 (gmt 0)

10+ Year Member




The regular-expressions patterns in your three rules must be designed to detect the cases where you desire to rewrite to the three different URLs.

Could you give an example? I am not sure I understand why it would not recognize that the rewriting is for two different php urls:

1/ /home-furnishing\.php?addition2=$1

2/ /home-furnishing-article\.php?addition3=$1

I tried to for example make 2/ as :

RewriteRule ^another-folder/(.*)\.html$ /home-furnishing-article\.php?addition3=$1

but it is not doing anything. I think I am missing the point. If you could provide an example if you dramatically help I think.

Thanks

jdMorgan

7:55 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have apparently misunderstood the function of RewriteRule.

The first entry following RewriteRule is the URL requested by the client browser. The second entry is the location on the content you want to server when that first URL is requested:


RewriteRule [i]requested_URL[/i] [i]new_URL[/i]

So, there is no difference between your first two rules. Both of them attempt to redirect <anything>.html. to <somepage>.php.

Please see the references cited in our charter [webmasterworld.com].

Jim

JayDev

7:02 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



I now understood. I simply created two different patterns instead:

1/ ^folder/(.*)\.html$ .........

2/ ^another-folder/(.*)\.html$.........

It works great now. Thank you for your help and patience.

This is a great forum.