Tom
Tom

Reputation: 12998

RewriteRule creating 500 Internal Server Error

I have the following in my .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=$1

What I'm trying to achieve is this:

When the URL www.example.com/directory/10 is visited, the page www.example.com/directory/?id=10 is displayed on the browser without altering the appearance of the URL.

The above code creates a 500 Internal server error though.

Does anyone know where I'm going wrong?

Upvotes: 13

Views: 40458

Answers (6)

anubhava
anubhava

Reputation: 786289

Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$

Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.

Change your code to this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^directory/(.*)$ directory/index.php?id=$1 [L,QSA,NC]

Above code has an extra RewriteCond %{REQUEST_FILENAME} !-f that will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.php will be a valid file.

Upvotes: 29

erwin
erwin

Reputation: 119

Just uncomment #LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Because by default it was disabled/commented

Upvotes: 0

William Desportes
William Desportes

Reputation: 1711

Another day searching for a strange error on Apache. Working on my Docker Apache 2.4.48 alpine container. But not in production.

Here is the difference (just a dot):

Not working on hosting provider

RewriteRule ^public/(.*)$ ./public/index.php?route=/$1 [L,QSA]

Working on hosting provider

RewriteRule ^public/(.*)$ /public/index.php?route=/$1 [L,QSA]

Upvotes: 0

Davi Sousa
Davi Sousa

Reputation: 3

If you are using CodeIgniter and is in error problems 500. Follow the solution.

So to delete the segment "index.php" of URLs in CodeIgniter, you need to do 2 things. The first is to edit the /system/application/config/config.php file, changing the value of index_page policy to empty:

$config['index_page'] = '';

The second step is to create a file .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

And that's it! From now on, the URLs of your site/system made with CodeIgniter will no longer have the thread (called "annoying" by some) "index.php".

Upvotes: 0

Brilliant-DucN
Brilliant-DucN

Reputation: 724

I have got the same issue and found that "rewrite" module is not yet enabled in my case. So I need to enable it and then restart apache server:

  • Enable "rewrite" module: sudo a2enmod rewrite
  • Then restart apache server: sudo service apache2 restart

Hope this will help anyone.

Upvotes: 19

Paul S
Paul S

Reputation: 1239

You should try adding a forward slash to the front:

RewriteRule ^/directory/(.*)$ directory/index.php?id=$1

I've been caught out with that before.

Alternatively use the RewriteLog and RewriteLogLevel to debug, and look at the Apache error and access logs for further info:

RewriteLogLevel 3
RewriteLog ${APACHE_LOG_DIR}/rewrite.log

That will leave a log file in your apache log directory. In my case that is /var/log/apache

Upvotes: 3

Related Questions