开发者

mod_rewrite for sub directory

开发者 https://www.devze.com 2023-03-20 06:24 出处:网络
I have a domain and a sub folder, called \"sub\"..inside \"sub\" is index.php which has this: $page = $_GET[\'page\'];

I have a domain and a sub folder, called "sub"..inside "sub" is index.php which has this:

$page = $_GET['page'];
switch ($page)
{
    case "main":
        echo "main";
        break;
    case "task_orders":
        echo "task_orders";
        break;
    case "team_members":
        echo "team_members";
        break;
    case "team_experience":
        echo "team_experience";
        break;
    case "quality_assurance":
        echo "quality_assurance";
        break;
    case "geographical_support":
        echo "geographical_support";
        break;
    case "contact":
        echo "contact";
        break;
    default:
     开发者_StackOverflow社区   echo "main";
}

my htaccess in "sub" is:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^sub/([^/\.]+)/?$ index.php?page=$1 [L]

when I click a link which looks like:

<li><a href="/sub/main" title="Main" class="current">Main</a></li>
<li><a href="/sub/task_orders" title="Task Orders">Task Orders</a></li> 
<li><a href="/sub/team_members" title="Team Members">Team Members</a></li> 
<li><a href="/sub/team_experience" title="Team Experience">Team Experience</a></li> 
<li><a href="/sub/quality_assurance" title="Quality Assurance Program">Quality Assurance</a></li> 
<li><a href="/sub/geographical_support" title="Geographical Support">Geographical Support</a></li> 
<li><a href="/sub/contact" title="Points of Contact">Points of Contact</a></li> 

I get a page not found rather than echoing out what page I am on, any ideas?

I also tried:

RewriteRule ^/sub/([^/\.]+)/?$ /sub/index.php?page=$1 [L]

and same result


You're almost there. You don't want to specify the leading / when writing the RewriteRule.

In the document root:

RewriteEngine on
RewriteRule ^sub/([^/\.]+)/?$ /sub/index.php?page=$1 [L]

From the /sub folder:

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


If your .htaccess is in a subdirectory, you don't have to write the full path in a RewriteRule.

So use this in your .htaccess in sub-directory:

RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

or in root's .htaccess:

RewriteRule ^sub/([^/\.]+)/?$ sub/index.php?page=$1 [L]
0

精彩评论

暂无评论...
验证码 换一张
取 消