配置.htaccess将HTTP GET参数更改为URI

时间:2013-05-29 01:01:11

标签: regex .htaccess mod-rewrite

我想问一个关于.htaccess的简单问题。我有index.html的表单,并希望将值传递给receiver.php。如何将.htaccess配置为输出http://localhost/demo/receiver/jibberish而不是http://localhost/demo/receiver.php?fid=jibberish

的http:// {HOST_NAME} /demo/index.html

<form method="get" action="receiver.php">
  FID <input type="text" name="fid" value="jibberish"> <br>
  <input type="submit" value="Submit"> 
</form>

的http:// {HOST_NAME} /demo/receiver.php

<?php 
  echo $_GET['fid'];
?>

我很难过。这是我的.htaccess代码。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^/receiver.php$ /receiver.php?fid=$1 [PT]
</IfModule>

2 个答案:

答案 0 :(得分:2)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(receiver)\.php\?fid=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(receiver)/(.+?)/?$ /$1.php?fid=$2 [L,NC,QSA]

验证一切正常后,将R=302替换为R=301。在测试mod_rewrite规则时,请避免使用R=301(永久重定向)。

答案 1 :(得分:1)

这应该在根目录下的一个.htaccess文件中起作用:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /demo/receiver.php\?fid=([^&\s]+) [NC]
RewriteRule  .*            /demo/receiver/%1?  [R=301,L,NC]

RewriteCond %{REQUEST_URI}  !receiver\.php [NC]
RewriteRule  ^demo/receiver/([^/]+)/?  /demo/receiver.php?fid=$1 [L,NC]