【问题标题】:.htaccess RewriteRule and $_GET variable.htaccess RewriteRule 和 $_GET 变量
【发布时间】:2011-12-20 15:52:04
【问题描述】:

我正在做一个网站,需要做一些 URL 重写

它与$_GET URL ReWriting 非常相似,但我似乎无法让它工作

基本上在我的index.php 中,我使用$_GET 变量来显示内容

<?php
$page = basename($_GET['page']).".php";
if(file_exists("content/".$page))
    include("header.php");
else
    include("header_index.php");
?>

<div id="main">
<!-- container -->
    <?php
    if (file_exists("content/".$page))
        echo '<div id="container">';
    else
        echo '<div id="container" class="index_container">';
    ?>
        <!-- content -->
            <div id="content">
                <?php
                    if(file_exists("content/".$page))
                        include("content/".$page);
                    else
                        include("content/index_page.php");
                    include("./content/right_menu.php");
                ?>
            </div>
        <!-- content -->
    </div>
<!-- /container -->
</div>

<?php
    include("./footer.php");
?>

这样test.com/?page=test1 会显示website_folder/content/test1.php

我正在尝试做的是使其成为test.com/test1

我页面上的所有链接都像&lt;a href="?page=testX"&gt;Test X&lt;/a&gt;

谢谢

编辑: 尝试重写规则
RewriteRule (.*)$ ?page=$1
RewriteRule ./([a-zA-Z]+)$ ./index.php?page=$1
RewriteRule /([a-zA-Z0-9]) /?page=$1
RewriteRule ^subdomain.test.com/subfolder/(.+) subdomain.test.com/subfolder/index.html?page=$1 [NC]
RewriteRule (.+)$ index.php?page=$1

【问题讨论】:

  • 我应该在.htaccess 中写什么才能得到test.com/test1 而不是test.com/?page=test1
  • 是的。这就像最简单的事情之一,类似于几乎每个关于 mod_rewrite 的教程中解释的内容,但它似乎不适用于我的情况。 (是的,我的主机确实允许 URL 重写,我知道我的 .htaccess 已被考虑在内)
  • 已编辑。标志大部分时间是[NC]

标签: php regex .htaccess


【解决方案1】:

应该这样做:

RewriteRule ^subdomain.test.com/subfolder/(.+) subdomain.test.com/subfolder/index.html?page=$1 [NC]

它对我有用!

(注意 .html。您可能需要将其更改为 .php、.asp 等。)

您还需要:

Options +FollowSymLinks
RewriteEngine on

在前面的陈述之上。

【讨论】:

  • 它似乎在这里不起作用...索引实际上位于 subdomain.test.com/subfolder/index 但它不应该改变任何东西 RewriteEngine on 已经存在 RewriteRule ^subdomain.test.com/subfolder/(.+) subdomain.test.com/subfolder/?page=$1 [NC] 没有工作
  • 我已经更新了答案。如果这不起作用,您能否详细说明尝试时会发生什么?
  • 我之前确实尝试过,但它也不起作用。请求 sub.test.com/subfolder/contact 使我变成 404,但 sub.test.com/subfolder/?page=contact 正确显示 website_folder/content/contact.php。所有 URL 仍显示为 sub.test.com/sub/?page=pagename
  • 如果这不起作用,我想我无法为您提供进一步的帮助。对不起:(
  • 没问题,我会想办法的。我不明白为什么它也不起作用,您的解决方案似乎完全合乎逻辑。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 2010-12-02
  • 2013-05-15
  • 2010-11-19
  • 2012-11-10
相关资源
最近更新 更多