1

?page=about在 index.php 的表单中使用 GET 方法调用(即“关于”是一个选择选项)。单击提交后,URL 将如下所示:

http://example.com/?page=about

但是我希望它看起来像这样:

http://example.com/about

但保持脚本(PHP)在 index.php 上工作(不要也不想有一个“关于”文件),即。发送表单后,我想在同一页面上使用 GET 值。我可以使 php 脚本工作,它反应很好,但 URL 重写根本不起作用。

我已经尝试了以下 htaccess 重写,但它没有做任何事情,URL 仍然如上。

我的 htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?page=$1 [L,QSA]

非常感谢您的帮助,因为我不知道为什么这不起作用。我在网上搜索过,每个人都指向这个规则,但它对我没有任何作用。

提前非常感谢!

编辑:

让我发布我使用的 php 代码,对不起,我错过了。我现在已经删除了该action属性,因为我在浏览器中使用它,我知道在提交表单时会将我发送到相同的浏览器:

<?php

$page = $_GET['page'];

switch($page){
 case 'about': echo 'I am me'; break;
 case 'galery': echo 'Nice pictures here'; break;
}

?>

<body>

<form action="/" method="get">
<select name="page">
    <option value="about">about</option>
    <option value="galery">galery</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>

编辑#2:

我必须添加action="/"到表单标签才能使 anubhava 的解决方案起作用。

4

2 回答 2

3

这应该是您完整的 .htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

#external redirect from /?page=about OR /index.php?page=about to /about
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php|)\?page=([^\s]+) [NC]
RewriteRule ^ /%1? [R=301,L]

#internal forward from /about to /?page=about
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)/?$ /?page=$1 [L,QSA]
于 2013-04-07T17:46:03.363 回答
0

尝试

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

我正在调用 ?page=about 在 index.php 上的表单中使用 GET 方法

尝试调用正确的链接?' /about' 而不是 ' ?page=about'

于 2013-04-07T17:36:02.217 回答