Forum Moderators: phranque

Message Too Old, No Replies

500 Internal Server Error when using htaccess

         

Evarest

11:11 pm on Nov 5, 2006 (gmt 0)

10+ Year Member



At the moment, i use htaccess to redirect urls to my index.php in the following way:

category/page.php
will be redirected to
index.php?cat=category&page=page.php&force=0&idx=1

I use the following rule for this:
RewriteRule ^([^/]+)/([^.]+).php$ /index.php?cat=$1&page=$2&force=0&idx=1 [L]

This works just fine. However, now i need to be able to add the following rules:

category/page -> index.php?cat=category&page=page.php&force=0&idx=1
category/page/ -> index.php?cat=category&page=page.php&force=0&idx=1
category/ -> index.php?cat=category&page=&force=0&idx=1
category -> index.php?cat=category&page=&force=0&idx=1

Offline (in a text editor), i'm able to simulate this behaviour using the following regular expression:
RewriteRule ^([^/]+)(/([^/^.]+)(/){0,1}){0,1}(/){0,1}$ /index.php?cat=$1&page=$2&force=0&idx=1 [L]

However, if i try to use this rule on my server, I get a 500 Internal Server Error...

I've tried different other configurations, but none worked (offline) like to one listed above...

Do i something wrong? And could anyone please point me in the correct direction? :)

Thanks!
Evarest

jdMorgan

12:31 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The regex in your rule is fairly impenetrable. If you would care to share how you think it works, and how you think it should work, we may be able to help.

First off, "[^/^.]" looks funny, and I suspect you may want "[^/.]". Then we have multiple instances of "{0,1}" which can be more efficiently replaced with "?".

A simpler approach might be to add two rules:


RewriteRule ^([^/]+)/([^/.]+)/?$ /index.php?cat=$1&page=$2&force=0&idx=1 [L]
RewriteRule ^([^/.]+)/?$ /index.php?cat=$1&page=&force=0&idx=1 [L]

As to why you got a 500 server error, I suspect that "index.php" would match your complicated rule, and end up being rewritten to itself in an 'infinite loop' until either the browser or the server reached its maximum redirection limit. See your server error log -- It will often give you the exact reason for the error.

Jim

Evarest

1:31 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



"As to why you got a 500 server error, I suspect that "index.php" would match your complicated rule, and end up being rewritten to itself in an 'infinite loop' until either the browser or the server reached its maximum redirection limit. See your server error log -- It will often give you the exact reason for the error. "

That seems to me a logical explanation :)

I'm not at home now, but i will check asap your rules:

RewriteRule ^([^/]+)/([^/.]+)/?$ /index.php?cat=$1&page=$2&force=0&idx=1 [L]
RewriteRule ^([^/.]+)/?$ /index.php?cat=$1&page=&force=0&idx=1 [L]

The idea is that when a user surfs to
[mydomain.com...]

the request is handled by index.php?cat=category&page=page&force=0&idx=1

Also when the user surfs to
[mydomain.com...]
this should happen.

Additionally, the user should get an overview of the pages in each category if he goes to
[mydomain.com...]

This can be done in my index.php if the user goes to
index.php?cat=category&page=&force=0&idx=1

Therefore, the htaccess should also contain a rule for this.

Lastly, the user can go to
[mydomain.com...]

This means that the category = category, page = page and additional parameters are force=forceid and index=1

So index.php should handle this as well:
index.php?cat=category&page=&force=forceid&idx=2

I tried to get this all done in several rules, but that didn't work. So i tried it in 1 rule which i created using a texteditor (that could handle regular expressions). That's how i came up to that complicated rule...

Thanks!