Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule - Numbers vs. full text URLs

         

whoisgregg

4:19 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So basically I have profile pages that are, by default, assigned a number with which they can accessed at a URL like http://example.com/profiles/42

Each user can then give their profile a "real name" using any combination of letters, numbers, etc. that is accessed at a URL like http://example.com/profiles/I_made_this%21

Also, for either form, appending ";public" to the profile URL should bring up a special version of the profile.

When I set up the rewrite for the numbers only, it works perfect. When I try to redirect the other form, I get 500 internal server error. :/

Works:

 Options +FollowSymlinks
RewriteEngine On
RewriteBase /profiles
RewriteRule ^new$ /profiles/new.php [L]
RewriteRule ^([0-9]+);public$ /profiles/index.v3.php?public&dd=$1 [L]
RewriteRule ^([0-9]+)$ /profiles/index.v3.php?dd=$1 [L]

Breaks:

 Options +FollowSymlinks
RewriteEngine On
RewriteBase /profiles
RewriteRule ^new$ /profiles/new.php [L]
RewriteRule ^([0-9]+);public$ /profiles/index.v3.php?public&dd=$1 [L]
RewriteRule ^([0-9]+)$ /profiles/index.v3.php?dd=$1 [L]
RewriteRule ^(.*);public$ /profiles/index.v3.php?public&uri=$1 [L]
RewriteRule ^(.*)$ /profiles/index.v3.php?uri=$1 [L]

jdMorgan

4:33 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 500-Server Error
What is in your server error log file? -- It will often tell you exactly what is wrong.

Best guess: There's nothing in your code (the last rule, specifically) to stop the rewritten URL from being rewritten again, so you probably have an 'infinite' rewriting loop. Exclude the 'correct' URLs from being re-rewritten by using a negative-match RewriteCond for those URLs.

Jim

whoisgregg

6:39 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maximum number of internal redirects reached. ...

Exactly as predicted. :) So, I modified it as follows:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /profiles
RewriteRule ^new$ /profiles/new.php [L]
RewriteRule ^([0-9]+);public$ /profiles/index.v3.php?public&dd=$1 [L]
RewriteRule ^([0-9]+)$ /profiles/index.v3.php?dd=$1 [L]
RewriteCond %{REQUEST_FILENAME}! -f
RewriteRule ^(.*);public$ /profiles/index.v3.php?public&uri=$1 [L]
RewriteCond %{REQUEST_FILENAME}! -f
RewriteRule ^(.*)$ /profiles/index.v3.php?uri=$1 [L]

Now it works perfectly! Thanks for your help Jim.

jdMorgan

6:57 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh! Don't use "-f" "file-exists" checking if you can possibly avoid it -- It's VERY inefficient.

I'm not sure why you needed to add an exclusion to the second-to-last rule -- It doesn't look recursive.

For the last rule, may I suggest:


RewriteCond $1 !^profiles/index\.v3\.php$
RewriteRule ^(.*)$ /profiles/index.v3.php?uri=$1 [L]

You may need to adjust this based on your RewriteBase.

Jim

whoisgregg

8:30 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought it would be inefficient but had decided to plow ahead anyway. Unfortunately, I've set up my architecture poorly (decisions going back for the last 12 months) to deal with this particular moment. The entire site (all supporting files) are actually in the /profiles directory. So the last suggestion caused images, javascript files, etc. to have the "maximum redirects" error.

What do you think of the approach below where I check for file endings? It seems to be working as I would expect it to. I'm going to forbid the user generated url from containing any of these strings.

RewriteCond $1!\.php$ 
RewriteCond $1!\.jpg$
RewriteCond $1!\.gif$
RewriteCond $1!\.png$
RewriteCond $1!\.css$
RewriteCond $1!\.js$
RewriteRule ^(.*)$ /profiles/index.v3.php?uri=$1 [L]

jdMorgan

11:59 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For use in .htaccess, use the "local OR" -- it's faster:

RewriteCond $1 !\.(php¦jpg¦gif¦png¦css¦js)$
RewriteRule (.*) /profiles/index.v3.php?uri=$1 [L]

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

whoisgregg

1:07 pm on Nov 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And more readable. Thanks for all your help Jim. :)