将带参数的url重定向到可读URL

时间:2014-04-30 13:14:28

标签: apache .htaccess mod-rewrite redirect seo

我最近使用以下htaccess将带有参数的网址更改为更具可读性的网址:

RewriteRule ^page/([A-Za-z0-9-+_\s]+)$ /index.php?n=$1 [L]
RewriteRule ^category/([A-Za-z0-9-+_\s]+)/([0-9]+)$ /index.php?p=$1&b=$2 [L]

我认为这会在搜索引擎上出现重复内容时出现问题,因为index.php网址和domain.com/page/网址都会被编入索引。

如果拨打domain.com/page/1的电话被重定向到index.php?n=1,我该如何将对index.php的请求重定向到domain.com/page/

我尝试这样做是为了给我一个404代码,将我发送到domain2。

#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC]
#RewriteRule ^index\.php$ http://domain.com/page/? [R=301,L]


htaccess的

IndexIgnore *
ErrorDocument 404 http://domain2.com


#REDIRECTS
Redirect 301 /keyword.php http://domain.com
Redirect 301 /inc/keyword.php http://domain.com




Options +FollowSymlinks
RewriteEngine on

#Redirect all pages in a folder to a single URL
RewriteRule ^test/(.*)$ http://domain.com [L,R=301]

# HTTP HOST, escape . and start string with ^
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,NC] 


RewriteCond %{QUERY_STRING} ^p=h&m=([A-Za-z]+)&y=([0-9]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com? [R=301,L]


#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC]
#RewriteRule ^index\.php$ http://domain.com/page/? [R=301,L]


RewriteRule ^page/([A-Za-z0-9-+_\s]+)$ /index.php?n=$1 [L]

RewriteRule ^category/([A-Za-z0-9-+_\s]+)/([0-9]+)$ /index.php?p=$1&b=$2 [L]

1 个答案:

答案 0 :(得分:0)

而不是您的评论301规则尝试这一个:

RewriteCond %{THE_REQUEST} /index\.php\?n=([^\s&]+)&y=([^\s&]+) [NC]
RewriteRule ^ /page/%1/%2? [R=301,L]

RewriteCond %{THE_REQUEST} /index\.php\?n=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]