Your Location is: Home > Php

.htaccess Link rewrite returns a 404 error

From: Tokyo View: 4692 Ghaith Ishaq 

Question

I have a link that looks like this:

https://www.example.com/news/foobar.php?id=14&artName=Hello-World

I want it to look like this:

https://www.example.com/news/foobar/14/Hello-World

This is what I currently have in my .htaccess code:

<IfModule mod_speling.c>
   CheckSpelling off
   CheckCaseOnly off
</IfModule>

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^(news/foobar)/([\w-]+)/(.+) $1.php?id=$2&artName=$3 [QSA,L,NC]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule !\.php$ %{REQUEST_URI}.php [QSA,L]

The link to the article looks like this:

<a href = "https://www.example.com/news/foobar/'.$row['id'].'/'.$row['linkAddress'].'">Article</a>

Where from the database:

$row['id'] = 14 and $row['linkAddress'] = Hello-World

When I go to:

https://www.redicate.com/news/foobar/14/Hello-World

I get a 404 error- not found

If I instead go to:

https://www.redicate.com/news/foobar.PHP/14/Hello-World

The website loads but the get variables are missing so the website looks broken.

I have no idea what is causing this 404 error and I have been working on this issue for sometime, I would appreciate any help I could receive, thank you.

Best answer

Not exactly yours, but here's a stripped down one that works for me.

<FilesMatch "^\.htaccess">
    Order allow,deny
    Deny from all
</FilesMatch>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^/?(.*)/(.*)$ news.php?id=$1&artName=$2 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

Also make sure SSL is enabled, setup and works. Hope it helps!