Question

I would like to create a simple page with pretty url and a two-level menu. I use a directory in localhost, but of course, on the server i don't (That's why i commented the RewriteBase). I've have the following .htaccess setup:

Options +FollowSymlinks
RewriteEngine on
#RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&type=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?page=$1 [L]

And there is the html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>htaccess test</title>
</head>
<body>
    <img src="img/image.png" alt="image" />
    <br />
    <a href="home">Home</a>
    <a href="aboutus">About us</a>
    <a href="gallery">Gallery</a>
    <a href="help">Help</a>
    <?php
        $page = isset($_GET['page']) ? $_GET['page'] : 'unknown';
        echo "<h1>$page</h1>";
        if ($page == 'gallery') {
            ?>
                <a href="gallery/funny">Funny</a>
                <a href="gallery/beauty">Beauty</a>
                <a href="gallery/interesting">Interesting</a>
            <?php
        }
        $submenu = isset($_GET['type']) ? $_GET['type'] : 'unknown';
        if ($submenu != 'unknown') {
            echo "<h2>$submenu</h2>";
        }
    ?>
    </body>
</html>

I want to use only 2 url parameter, the page and the type. The page should be the first directory in the pretty url, the type the second.

The main menu works great. When I click on a sub-menu in the Gallery the page loads but the images don't load as the parameter is added to their paths. When I click on another submenu, the page parameter is duplicated in the url.

I know it must be a pretty easy step but i'm completely stuck.

Was it helpful?

Solution

The problem is that the links will be interpreted as relative to the current url path. So you either have to provide an appropriate <base> tag or create the links as absolute (e.g. /gallery/funny).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top