htaccess SEO友好的网址

时间:2014-06-05 11:13:52

标签: php apache .htaccess mod-rewrite

我对特定网站的网址如下所示:

http://www.mytestingsite123.com/play.php?alias=this-is-an-alias#.U5BPYvl_tNo

但我想要这个网址:

http://www.mytestingsite123.com/this-is-an-alias

http://www.mytestingsite123.com/video/this-is-an-alias

因此它将更加SEO友好。能帮到我吗,我怎么能通过修改.htaccess文件来做到这一点。

我的.htaccess文件包含以下内容:

RewriteEngine On

# whole domains
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

# subdomains
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliases (if misssing)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomains (if misssing)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

1 个答案:

答案 0 :(得分:1)

您需要附加.htaccess这样的文件:

RewriteRule ^video/([^/]+)?$ play.php?alias=$1 [L]
RewriteRule ^([^/]+)?$ play.php?alias=$1 [L]

第一条规则将匹配 http://www.mytestingsite123.com/video/this-is-an-alias

第二条规则将匹配 http://www.mytestingsite123.com/this-is-an-alias

[L]告诉apache停止查找规则,您可以将其更改为[L,QSA],这意味着如果您在新uri的末尾添加额外的参数,它将正确传递它们。< / p>