使用LiteSpeed对.htaccess中的mod_rewrite进行故障排除

时间:2013-12-17 23:39:30

标签: .htaccess mod-rewrite litespeed

我在这些位置有几个网页:

Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact

我正在尝试将它们重写为更漂亮的网址:

codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact

我已经确认我的htaccess文件有效并且mod-rewrite适用于google,但我无法在多个教程中指定我的语法。

  RewriteEngine on
  RewriteRule /home /index.php?page=home
  RewriteRule /([a-Z]+) /index.php?page=$1
  RewriteRule /education /index.php?page=home&filter=1
  RewriteRule /skills /index.php?page=home&filter=2
  RewriteRule /projects /index.php?page=home&filter=3
  RewriteRule /experience /index.php?page=home&filter=4

如何修复我的语法以将这些页面重写为更漂亮的网址?

1 个答案:

答案 0 :(得分:1)

你应该做的第一件事是修复你的正则表达式。您不能使用[a-Z]这样的范围,只需执行[a-z]并使用[NC](无大小写)标记。此外,您希望此规则位于最终,因为它会匹配/projects的请求,这将使得规则进一步向下将永远不会被应用。然后,你想摆脱所有领先的斜杠。最后,您需要正则表达式的边界,否则它将匹配index.php并导致其他错误。

所以:

  RewriteEngine on
  RewriteRule ^home /index.php?page=home
  RewriteRule ^education /index.php?page=home&filter=1
  RewriteRule ^skills /index.php?page=home&filter=2
  RewriteRule ^projects /index.php?page=home&filter=3
  RewriteRule ^experience /index.php?page=home&filter=4
  RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]