Suyash
Suyash

Reputation: 625

.htaccess throws 500 internal error with rewrite condition for non existing directory

I'm trying to make a facebook like user profile page using .htaccess ie. http://example.com/<userid > will actually call http://example.com/sites/<userid >/<sub_page > using thefollowing code:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]

Options -Indexes

ErrorDocument 404 /missing.html
ErrorDocument 403 /forbidden.html

It works perfectly well when the folder sites/<userid> exists but when it doesn't it throws 500 internal error. I've scoured the internet but couldn't go beyond this. Can someone please help with this. Can i have a text file with all existing folders and somehow use that to generate a list in the .htaccess?

Thanks in advance!! :)

Upvotes: 1

Views: 232

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

Your rewrite rules are looping. The regular expression ^([^/]+)/?(.*)$ is matching the rewritten URI sites/something/something and it's looping indefinitely (so your URI starts looking like: sites/sites/sites/sites/sites/sites/sites/sites/sites/something/something etc).

Either tweak your regular expression so that it doesn't liberally match what's after the first ([^/]+)/? expression or add an additional condition:

RewriteCond %{REQUEST_URI} !^/sites/

Upvotes: 1

Related Questions