htaccess seo-friendly URL' s

时间:2014-11-16 12:31:47

标签: .htaccess mod-rewrite url-rewriting seo friendly-url

我需要一些帮助才能在我的网站上获得友好的网址。

这些是我需要重写的链接:

www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number

这是我的.htaccess。我解决了第一个链接的问题,但是我无法为第二个链接执行此操作。

我想要第二个链接:

www.example.com/profile/name or
www.example.com/profile/id

这是我的.htaccess文件:

# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]

# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

我上面提交的代码解决了我的问题。

1 个答案:

答案 0 :(得分:0)

您可以使用:

# for www.example.com/index.php?page=profile&id=number
RewriteCond %{QUERY_STRING} page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=page
RewriteCond %{QUERY_STRING} page=([^&]+)
RewriteRule ^ /%1? [L,R=301]

重写

# rewrites back
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]