.htaccess短而友好的网址

时间:2012-06-21 20:17:03

标签: php apache .htaccess mod-rewrite

我正在寻找合适的.htaccess规则来制作类似

的Facebook.com/user网址

如果我访问例如

,我想要什么
www.domain.com/StackOverFlow

将重写为

www.domain.com/index.php?category=StackOverFlow

或者

www.domain.com/category.php?item=StackOverFlow

有任何帮助吗?我在这里尝试了一些问题,但他们没有明确表达这种行为。

提前致谢

编辑:

忘了说我只想使用此规则重定向,域之后的任何内容都没有任何扩展名

例如,它应该重定向www.domain.com/categoryDescription但不能www.domain.com/categoryDescription.phpwww.domain.com/categoryDescription.html

3 个答案:

答案 0 :(得分:3)

尝试:

RewriteRule ^StackOverFlow$ index.php?category=StackOverFlow [NC,L]

或:

 RewriteRule ^StackOverFlow$ category.php?item=StackOverFlow [NC,L]

通常:

RewriteRule ^([^/]*)$ index.php?category=$1 [NC,L]

或:

 RewriteRule ^([^/]*)$ category.php?item=$1 [NC,L]

答案 1 :(得分:3)

以下示例使用.com/之后的URI并将其分配给$1。 你可以为“item”

做同样的事情
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?category=$1 [L]

答案 2 :(得分:3)

<IfModule mod_rewrite.c>
  RewriteEngine on
  # Rewrite URLs of the form 'x' to the form 'index.php?category=x'.
  RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
</IfModule>
相关问题