Your Location is: Home > Php

.htaccess Link rewrite returns a 404 error

From: Tokyo View: 4692 Ghaith Ishaq 


I have a link that looks like this:

I want it to look like this:

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

<IfModule mod_speling.c>
   CheckSpelling off
   CheckCaseOnly off

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 = "'.$row['id'].'/'.$row['linkAddress'].'">Article</a>

Where from the database:

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

When I go to:

I get a 404 error- not found

If I instead go to:

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

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!