【问题标题】:.htaccess doesn't work with $_GET.htaccess 不适用于 $_GET
【发布时间】:2012-06-03 21:40:14
【问题描述】:

我在 .htaccess 文件中有一行:

RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

和 index.php 中的一行:

$page = $_GET['page']; echo $page;

如果我转到http://www.example.com/test-page,它会返回 index.php。

我发现修复它的唯一方法是:

RewriteRule ^(.*)/$ index.php?page=$1 [NC,L]

如果我去http://www.example.com/test-page/ 它可以工作并输出测试页。

但是,我不希望网页使用http://www.example.com/test-page/,我希望它使用http://www.example.com/test-page

我该如何解决这个问题,最好不要在内部重写中添加在 url 末尾添加 / 的规则...?

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    试试

    RewriteRule ^(.*)$ /index.php?page=$1 [NC,L]
    

    或尝试类似的方法

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    这会将所有流量重定向到 index.php。您可以使用 e。 G。 $_SERVER[REQUEST_URI] 获取您的路径。

    【讨论】:

    • 很多网页都使用类似的代码。可能是因为我需要启用 Apache mod 吗?我打开了 mod_rewrite。
    • 如果上面的代码有效,那么 mod_rewrite 应该可用。
    • 嗯,感谢您的更新。但后来我想添加诸如之类的东西
    • 为什么需要 NC 标志?只用[L],不就是做你想做的事吗?
    • RewriteRule ^shop/(.*)/(.*)$ shop.php?page=$1&product=$2 [NC,L]
    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 2013-10-23
    • 2014-03-19
    • 2015-11-10
    • 2014-09-30
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多