So, I have a site http://example.com
and i have a db with articles and what not,
When the article id is http://example.com/article
, everything is fine, the rewrite is done correctly, but when the ID is is http://example.com/article-name
I get a 404.
This is the code:
.htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9] )$ index.php?ID=$1
RewriteRule ^c/([^/] )?$ index.php?CAT=$1 [L,QSA]
RewriteRule ^topic/([^/] )?$ index.php?topic=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.] )$ $1.php [NC,L]
article.php
<?php
if (isset($_GET['ID'])) {
require_once 'con.php';
$ID = mysqli_real_escape_string($conn, $_GET['ID']);
$sql = "SELECT * FROM `blog_article` WHERE articleID = '$ID' ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
if (mysqli_num_rows($result) == 0) {
header("Location: /");
}
else{
include 'article.php';
}
}
elseif (isset($_GET['CAT'])) {
include 'c.php';
}
elseif (isset($_GET['topic'])) {
include 't.php';
}
else {
include 'index_view.php';
}
?>
CodePudding user response:
This rewrite rule:
RewriteRule ^([a-zA-Z0-9] )$ index.php?ID=$1
Means "if you see 1 or more letter or number in a sequence, rewrite it to index.php?ID=the_sequence_that_was_found"
The rule doesn't include any punctuation characters such as the -
. If you'd like it to consider capturing letters, numbers and dashes, then the line should instead be like this:
RewriteRule ^([a-zA-Z0-9-] )$ index.php?ID=$1
Note that if you want to add more characters in here, you may need to escape them. This is because characters like the dash are a special character for these regex patterns, so a backslash tells Apache that it should be a literal dash like so:
RewriteRule ^([a-zA-Z0-9\-] )$ index.php?ID=$1