Search code examples
.htaccessmod-rewrite

How to fix .htaccess mod_rewrite server error on localhost


Context

I have a website where users can create articles. I'm trying to use mod_rewrite to make my links include the title of the article for SEO purposes. To start, though, I'm just trying to get the ID of the article and users in the rewritten URL because that's how I access the article. I'm new to regular expressions and editing file configuration in general, so I've been trying to understand what's going on with these mod_rewrite lines. I'm still left failing to understand why the code doesn't work.

Problem

I have a URL on my localhost, localhost/Real%20Website/readArticles.php?article_id=86&userid=64.

I want to change this to localhost/Real%Website/readArticles/86/64.

To do so, I added the following code to my .htaccess file:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^readArticles/([0-9+])/([0-9]+) readArticles.php?article_id=$1&userid=$2 [NC, L]

With this code, I get the following error:

Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.

Error 500
localhost
Apache/2.4.46 (Unix) OpenSSL/1.1.1h PHP/7.4.12 mod_perl/2.0.11 Perl/v5.32.0

I'm unsure what the problem is here. Is it because I have to account for the Real Website folder?

Question

How do I fix this server error with the code given above?

Let me know if there are any questions.


Solution

  • As @arkascha pointed out, [NC, L] has an extra space; it should be [NC,L]. This fixed the issue.