Currently I have my htaccess placed in my subdirectory where the slakken.php is also placed, this is the code:
RewriteEngine on
RewriteBase /REA/MySQL/06-permalinks/opdracht1/
# Zt ervoor dat /slakken/spaanse-wegslak wordt omgeleid naar slakken.php?slug=spaanse-wegslak
RewriteRule ^([a-z0-9-]+)/?$ slakken.php?slug=$1 [L,QSA]
So, its placed in that directory based from my xampp. But when I click on a link in my slakken.php, it gives me a 404 not found. This is my slakken.php:
<?php
if (isset($_GET['slug']) && !empty($_GET['slug'])) {
$slug = $_GET['slug'];
} else {
$slug = 'wijngaardslak';
}
$user = 'root';
$pass = '';
try {
$dbh = new PDO('mysql:host=localhost;dbname=mysql_les6;charset=utf8', $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT common_name, scientific_name, family, description, image_src FROM snails WHERE slug = ?");
$stmt->bindParam(1, $slug, PDO::PARAM_STR);
$stmt->execute();
$info = $stmt->fetch(PDO::FETCH_ASSOC);
$dbh = null;
$stmt = null;
}
catch (PDOException $e) {
echo '<p>Oeps! Er is iets foutgegaan!</p>';
file_put_contents('PDOErrors.txt', $e->getMessage().PHP_EOL, FILE_APPEND);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>De Slakkentuin - <?=$info['common_name'];?></title>
<link rel="canonical" href="/<?=$slug;?>">
<link rel="stylesheet" href="css/stylesheet.css">
</head>
<body>
<nav>
<p>
<a href="/slakken/wijngaardslak">Wijngaardslak</a> |
<a href="/slakken/gewone-tuinslak">Gewone tuinslak</a> |
<a href="/slakken/spaanse-wegslak">Spaanse wegslak</a> |
<a href="/slakken/egel-wegslak">Egel-wegslak</a> |
<a href="/slakken/zwarte-wegslak">Zwarte wegslak</a> |
<a href="/slakken/gevlekte-akkerslak">Gevlekte akkerslak</a> |
<a href="/slakken/grote-poelslak">Grote poelslak</a> |
<a href="/slakken/karakol">Karakol</a>
</p>
</nav>
<section>
<?php if (!empty($info)) { ?>
<h1><?=$info['common_name'];?> <span class="scientific">(<?=$info['scientific_name'];?>)</span></h1>
<p>Familie: <?=$info['family'];?></p>
<p><?=$info['description'];?></p>
<img src="images/<?=$info['image_src'];?>" alt="<?=$info['scientific_name'];?>" title="<?=$info['common_name'];?>" width="512" height="384">
<?php } else { ?>
<p>Sorry, er zijn geen gegevens gevonden.</p>
<?php } ?>
</section>
</body>
</html>
Anyone knows what could be the issue?
Currently I have my htaccess placed in my subdirectory where the slakken.php is also placed, this is the code:
RewriteEngine on
RewriteBase /REA/MySQL/06-permalinks/opdracht1/
# Zt ervoor dat /slakken/spaanse-wegslak wordt omgeleid naar slakken.php?slug=spaanse-wegslak
RewriteRule ^([a-z0-9-]+)/?$ slakken.php?slug=$1 [L,QSA]
So, its placed in that directory based from my xampp. But when I click on a link in my slakken.php, it gives me a 404 not found. This is my slakken.php:
<?php
if (isset($_GET['slug']) && !empty($_GET['slug'])) {
$slug = $_GET['slug'];
} else {
$slug = 'wijngaardslak';
}
$user = 'root';
$pass = '';
try {
$dbh = new PDO('mysql:host=localhost;dbname=mysql_les6;charset=utf8', $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT common_name, scientific_name, family, description, image_src FROM snails WHERE slug = ?");
$stmt->bindParam(1, $slug, PDO::PARAM_STR);
$stmt->execute();
$info = $stmt->fetch(PDO::FETCH_ASSOC);
$dbh = null;
$stmt = null;
}
catch (PDOException $e) {
echo '<p>Oeps! Er is iets foutgegaan!</p>';
file_put_contents('PDOErrors.txt', $e->getMessage().PHP_EOL, FILE_APPEND);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>De Slakkentuin - <?=$info['common_name'];?></title>
<link rel="canonical" href="http://deslakkentuin.nl/slakken/<?=$slug;?>">
<link rel="stylesheet" href="css/stylesheet.css">
</head>
<body>
<nav>
<p>
<a href="/slakken/wijngaardslak">Wijngaardslak</a> |
<a href="/slakken/gewone-tuinslak">Gewone tuinslak</a> |
<a href="/slakken/spaanse-wegslak">Spaanse wegslak</a> |
<a href="/slakken/egel-wegslak">Egel-wegslak</a> |
<a href="/slakken/zwarte-wegslak">Zwarte wegslak</a> |
<a href="/slakken/gevlekte-akkerslak">Gevlekte akkerslak</a> |
<a href="/slakken/grote-poelslak">Grote poelslak</a> |
<a href="/slakken/karakol">Karakol</a>
</p>
</nav>
<section>
<?php if (!empty($info)) { ?>
<h1><?=$info['common_name'];?> <span class="scientific">(<?=$info['scientific_name'];?>)</span></h1>
<p>Familie: <?=$info['family'];?></p>
<p><?=$info['description'];?></p>
<img src="images/<?=$info['image_src'];?>" alt="<?=$info['scientific_name'];?>" title="<?=$info['common_name'];?>" width="512" height="384">
<?php } else { ?>
<p>Sorry, er zijn geen gegevens gevonden.</p>
<?php } ?>
</section>
</body>
</html>
Anyone knows what could be the issue?
Share Improve this question asked Mar 10 at 14:53 T. LimptT. Limpt 411 bronze badge 2 |1 Answer
Reset to default 1RewriteEngine on RewriteBase /REA/MySQL/06-permalinks/opdracht1/ # Zt ervoor dat /slakken/spaanse-wegslak wordt omgeleid naar slakken.php?slug=spaanse-wegslak RewriteRule ^([a-z0-9-]+)/?$ slakken.php?slug=$1 [L,QSA]
Your RewriteBase
directive looks (very) wrong. This should be a root-relative URL-path. However, what you have here looks like an absolute filesystem-path? But what is the "subdirectory" where your files are located? I assume from your PHP code and URLs this must be /slakken
- but this doesn't match your RewriteBase
at all??
UPDATE: The effect of using RewriteBase
as set above would be to rewrite the request to /REA/MySQL/06-permalinks/opdracht1/slakken.php
- which presumably does not exist, hence the 404.
The purpose of the RewriteBase
directive is to control how relative substitution strings (2nd argument to the RewriteRule
directive) are rewritten. The URL-path given in the RewriteBase
directive is used as a prefix. This is all it does.
However, if the .htaccess
and slakken.php
files are located in the same directory then the RewriteBase
directive is not required and should probably be removed entirely (unless you have some other requirements). Since relative substitution strings are written relative to the directory that contains the .htaccess
file.
But your code does not handle requests for the subdirectory itself? Is this a requirement? Or do you only need to access /slakken/<something>
and not /slakken/
only? I would assume you did need to access the directory (the homepage and not have to enter slakken.php
), so you would need to set DirectoryIndex slakken.php
.
Your rule also accepts an optional trailing slash. This potentially allows for a duplicate content issue since these are two separate URLs and they both serve the same content. If you need to handle both requests then one should redirect to the other. From your HTML, the canonical URLs do not end in a trailing slash.
Try the following instead:
DirectoryIndex slakken.php
RewriteEngine on
# (Optional) Remove trailing slash from non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/$ /$1 [R=301,L]
# Zt ervoor dat /slakken/spaanse-wegslak wordt omgeleid naar slakken.php?slug=spaanse-wegslak
RewriteRule ^([a-z0-9-]+)$ slakken.php?slug=$1 [L,QSA]
Note that, as per your original rule, this only handles URLs that are 1 path-segment deep (beyond the subdirectory). (And does not prevent requests to slakken.php
directly.)
UPDATE:
<link rel="stylesheet" href="css/stylesheet.css">
COMMENT: Only thing is that the css and images are not working. The only way I can get them to work is to give them a base url via PHP, like $base_url = "/REA/MySQL/06-permalinks/opdracht1/"; is that a correct way? Or is there a better way?
Since the current URL of the page in the browser is of the form /slakken/wijngaardslak
then the browser will naturally resolve a relative URL like css/stylesheet.css
to /slakken/css/stylesheet.css
. If this is not desirable then you have 3 options:
(Preferred) Use root-relative URLs (starting with a slash) in your HTML links, as you suggest here.
Use a
base
element in thehead
section of the HTML to indicate the base URL that all relative URLs are relative to. For example:<base href="https://example/REA/MySQL/06-permalinks/opdracht1/">
However, any in-page anchors are also relative to this base-URL.
Use additional rules in
.htaccess
to internally rewrite requests for your static assets to the real URL target. This effetively "hides" the real file location from the user. However, this can get messy if you have URLs (pages) at different URL-path depths (you don't in your example). For example:# Rewrite "/css/..." and "/images/..." requests RewriteRule ^(css|images)/.+\.(css|jpg|webp|gif)$ /REA/MySQL/06-permalinks/opdracht1/$0 [L]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840009a4596498.html
AllowOverride
configurations for that location. – arkascha Commented Mar 10 at 18:19