2

So I know this question has been asked 20 other times and 20 different ways but after all of my researching I still can't seem to figure this out. My setup works great until I type in a URL with a trailing slash and then I get 404 errors. For example, when I type in the URL mydomain.com/first and hit enter, it works fine. When I type in mydomain.com/first/ and hit enter, it works fine then too. But then, if I type in mydomain.com/second it sends me to mydomain.com/first/second and throws a 404 error.

Here is my entire htaccess page:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ index.php?url=$1 [NC,L]
</IfModule>

My index page:

<?php
define('ROOT_DIR', dirname(__FILE__) . '/'); // Define the root directory for use in includes 
require_once (ROOT_DIR . 'library/bootstrap.php');

$url = strtolower($_GET['url']);

include(ROOT_DIR . 'views/headerView.php');

switch($url)
{
    case "first": include(ROOT_DIR . 'views/firstPageView.php');
        break;
    case "second": include(ROOT_DIR . 'views/secondPageView.php');
        break;
    default: include(ROOT_DIR . 'views/homeView.php');
} 

include 'views/footerView.php';
?>

And the default homeView template:

<p>This is the home page.</p>
<p>To the first page. <a href="first">First Page</a></p>
<p>To the second page. <a href="second">Second Page</a></p>

I would think that since the slash is optional and not part of the back reference, that the slash would be left off the URL after the page load but in my above example, its clearly not. Any advice would be most appreciated. Thank you

4

3 回答 3

1

Here is what should work:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ /myProject/index.php?url=$1 [NC,L]
</IfModule>

You need to put in the / in front of index.php.

Otherwise Apache will look for the index.php file in your current directory.

于 2012-08-29T10:25:15.133 回答
0

Hope this will help

RewriteRule ^([A-Za-z0-9\_\-]*)/?$ index.php?url=$1 [L] 
于 2012-08-30T01:08:53.963 回答
0

your mod_rewrite config is ok, the problem is on the client. if current path is /foo/first, <a href="second"/> will lead to /foo/second. if it's /foo/first/, the same <a href/> will lead to /foo/first/second.

于 2012-08-30T07:20:51.253 回答