URL重写/index.php?page=sample到/sample.htm

时间:2013-12-09 12:35:22

标签: php regex apache .htaccess mod-rewrite

.htaccess非常新,不完全了解它的大部分内容。无论如何,目的是将/index.php?page=sample重写为sample.htm。 (或任何其他页面)

下面的脚本似乎部分有效。如果我输入/sitemap.htm,它会显示该页面,但地址栏会更改为/index.php?page=sitemap。如何将url重写为更友好的url,以便当我输入/sitemap.htm时,地址保持这种状态,当我键入/index.php?page=sitemap时,它将重定向到/sitemap.htm但仍然有效? / p>

道歉,如果我听起来很混乱。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+getreal\.co\.uk/index\.php\?page=([^&\s]+) [NC]
RewriteRule ^ %1.htm? [L,R=301]

RewriteRule ^(.+?)\.htm?$ index.php?page=$1 [L,R=301]

1 个答案:

答案 0 :(得分:1)

你非常接近。但THE_REQUEST不包含域名。

试试这段代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} /index\.php\?page=((?!login\b)[^&\s]+) [NC]
RewriteRule ^ %1.htm? [L,R=302]

RewriteRule ^(.+?)\.htm$ index.php?page=$1 [L,NC,QSA]