.htaccess redirection rule for site

2019-08-02 19:55发布

I need help with htaccess URL rewriting for my site.

Problem is that just first page in some category has nice cruft free URL, and all other pages has dirty URLs.

For example first page has link like this:

http://www.my-site-name.com/Category-name/Subcategory-name/

But page 2, and any other pages are like this:

http://www.my-site-name.com/showcat.php?cat=Category-name&subcat=Subcategory-name&page=2
http://www.my-site-name.com/showcat.php?cat=Category-name&subcat=Subcategory-name&page=3

So, I need some SEO friendly htaccess redirect rule to make URLs like this:

http://www.my-site-name.com/Category-name/Subcategory-name-page-X/

or like this

http://www.my-site-name.com/Category-name/Subcategory-name/X/

But probably I would like first more.

This is part of .htaccess for that: (I just added full content, maybe there is something more about it.

## For top rated items
RewriteRule ^top/page/(.*)/$ top.php?page=$1 [L]

## For latest items
RewriteRule ^latest/recent-page-(.*)/$ latest.php?page=$1 [L]

## For show most rated - most clicked - most downloaded and most searched items
RewriteRule ^most-rated.html$ showmost.php?type=1 [L]
RewriteRule ^most-clicked.html$ showmost.php?type=2 [L]
RewriteRule ^most-downloaded.html$ showmost.php?type=3 [L]
RewriteRule ^most-Searched.html$ showmost.php?type=4 [L]

## For showing category of item
RewriteRule ^(.*)/$ showcat.php?cat=$1 [L]

## For showing subcategory of item
RewriteRule ^(.*)/(.*)/$ showcat.php?cat=$1&subcat=$2 [L]

## For showig item
RewriteRule ^(.*)/(.*)/(.*).html$ show.php?cat=$1&sub_cat=$2&img=$3&rewrite=true [L]

## this section should be inserted just after the showing item rule above
#if the query string has cat, sub_cat and Img
RewriteCond %{QUERY_STRING} ^cat=(.+)&sub_cat=(.+)&img=(.+)$ [NC]
#and it is for resource show.php, then 301 redirect to Keyword rich URL
RewriteRule ^show\.php$ http://www.my-site-name.com/%1/%2/%3.html? [NC,L,R=301]

标签: .htaccess url
1条回答
我只想做你的唯一
2楼-- · 2019-08-02 20:32

I also cleaned it up a little

#don't rewrite if file or dir exists
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

## For showing category of item
RewriteRule ^([^/]*)/$ showcat.php?cat=$1 [L]

## For showing paged subcategory of item
RewriteRule ^([^/]*)/([^/]*)-page-([0-9]+)/$ showcat.php?cat=$1&subcat=$2&page=$3 [L]

## For showing subcategory of item
RewriteRule ^([^/]*)/([^/]*)/$ showcat.php?cat=$1&subcat=$2&page=1 [L]

## For showig item
RewriteRule ^([^/]*)/([^/]*)/([^/]*).html$ show.php?cat=$1&sub_cat=$2&img=$3&rewrite=true [L]
查看更多
登录 后发表回答