.htaccess - Htaccess not working in subdirectory, how to solve it? - Stack Overflow

Currently I have my htaccess placed in my subdirectory where the slakken.php is also placed, this is th

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
  • Could be that you fot to enable to interpretation of such distributed configuration files in the central configuration of your http server. Check your AllowOverride configurations for that location. – arkascha Commented Mar 10 at 18:19
  • Otherwise enable rewrite logging to learn what exactly is going on inside your rewriting engine. Check the documentation on how to do that. – arkascha Commented Mar 10 at 18:19
Add a comment  | 

1 Answer 1

Reset to default 1
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]

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:

  1. (Preferred) Use root-relative URLs (starting with a slash) in your HTML links, as you suggest here.

  2. Use a base element in the head 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.

  3. 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信