Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite rule!

mod_rewrite rule!

         

nevsk

6:07 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Hi,

i need some help im trying to create a rule to redirect [mydomain...] to [mydomain...] user letter'/user

i tryied the folloing rule:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /users/$2/$1$3

Can anyone help me?

jdMorgan

6:58 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nevsk,

Assuming that this code goes into httpd.conf, and that all usernames start with "~", what you've got should work in most cases. You could make it a little more efficient, prevent recursion, and remove the trailing slash ambiguity with:


RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^/~(([a-z])[^/]+)(.*) /users/$2/$1$3 [L]

Please tell us *how* the code doesn't work. Otherwise, about all we'll be able to do is spot syntax errors.

For those just tuning in to mod_rewrite, back-references are assigned according to the left parenthese's nesting order. Therefore, nevsk's $2 contains only the first letter of the username, while $1 contains the entire username -- that is, everything up to the first slash.

Jim

nevsk

7:19 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



jdMorgan,

My mistake... my users names dont start with ~ and im trying to add this rule to the .htaccess inside my home directory.

Tks,
nevsk

jdMorgan

9:02 pm on Jun 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, then remove "/~" from the RewriteRule pattern, leaving the leading slash on the RewriteCond pattern, and it should work better.

www.example.com/petra/page1.html -> www.example.com/users/p/petra/page1.html :


RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^(([a-z])[^/]+)(.*) /users/$2/$1$3 [L]

Jim

nevsk

10:12 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



Thats my .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/users
RewriteRule ^(([a-z])[^/]+)(.*) /users/$2/$1$3 [L]

But i still receiving http error 404...

am i missing something?

nevsk

10:27 pm on Jun 13, 2004 (gmt 0)

10+ Year Member



sorry.. its working, it took a few minutes to start working..

But theres anyway to prevent the root file from being rewrited, and also some directories like /images?

Tks!
nevsk

nevsk

12:16 am on Jun 14, 2004 (gmt 0)

10+ Year Member



Tks jdmorgan for your help!

im using the following rules and it appears to be working

RewriteEngine On
RewriteCond %{REQUEST_URI}!^/(cgi-bin¦css¦images¦include¦paypal¦scripts¦vdaemon¦users)/
RewriteCond %{REQUEST_URI}!\.(php¦gif¦jp?eg¦png)$
RewriteRule ^(([a-z])[^/]+)(.*) /users/$2/$1$3 [L]

Tks again!
nevsk