使用mod_rewriteredirect.htaccess的规则

我有一个PHP脚本根据查询从数据库dynamic生成内容。 用户可以从文本框中search用户名“mark”。 这些内容由PHP在www.domain.com/?username=mark提供,不过我希望用户看到提供的URL为www.domain.com/username/mark.html。 同样,用户可能会search一个configuration文件(整数)。 该内容在www.domain.com/?profile=2上生成。 我希望用户看到提供的URL为www.domain.com/profile/2.html。 这是可行的吗?

这是我的.htaccess文件:

RewriteEngine On RewriteRule ^username/([^/]*)\.html$ /?username=$1 [L] RewriteRule ^realname/([^/]*)\.html$ /?realname=$1 [L] RewriteRule ^profile/([^/]*)\.html$ /?profile=$1 [L] 

Mod_rewrite已启用,但该文件似乎不工作。

你的代码应该工作,也许根本不执行。 你可以尝试打字错误,看看Apache是​​否给出了500的回应。

除此之外,您可以尝试使用以下htaccess。 新增了index.php,以确保正确的文件被调用。

 RewriteEngine On RewriteRule ^username/([^/]*)\.html$ /index.php?username=$1 [L] RewriteRule ^realname/([^/]*)\.html$ /index.php?realname=$1 [L] RewriteRule ^profile/([^/]*)\.html$ /index.php?profile=$1 [L]