重写 .htaccess 文件中查询字符串 URL 的条件


Rewrite Condition for Query string URL in .htaccess file

我对.htaccess文件没有很好的了解,无论如何,我在某处找到了如下所示的代码:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

我正在将表单值发布到页面数据.php从索引.php页面,如下所示:

<!-- index.php page -->
<form name="testform" id="testform" method="post" action="pagedata">
<input type="text" name="firstname">
<input type="text" name="age">
<input type="text" name="city">
</form>

然后数据就好了...但是现在我需要更改 post 方法来获取方法。

如果我使用 get 方法提交表单,则 URL 如下,http://example.com/pagedata.php?firstname=test&age=26&city=somecity

但是我需要按如下方式转换上面的URL,http://example.com/pagedata/test/26/somecity

怎么做..?请指教。

有一个额外的规则:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}'s/+(pagedata)'.php'?firstname=([^&]*)&age=([^&]*)&city=([^'s&]*) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
RewriteRule ^(pagedata)/([^/]*)/([^/]*)/([^/]*)/?$ /$1.php?firstname=$2&age=$3&city=$4 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
</IfModule>

为您推荐:阿帕奇mod_rewrite简介

使用 mod_rewrite 创建人性化的 URL

例如,这可以帮助您通过以下方式 www.site.ru/product/123 www.site.ru/product.php?id=123 转换:

RewriteEngine on
RewriteRule ^product/([^/'.]+)/?$ product.php?id=$1 [L]