dongmi1221 2011-11-08 15:43
浏览 59
已采纳

apache htaccess为seo友好重写url

I am trying and failing at the moment to rewrite my urls for seo friendliness. Can you point out where I might be going wrong?

I'm trying to rewrite this url:

http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce

To this:

http://www.chillisource.co.uk/product/Grocery/Daves%20Gourmet/1/B0000DID5R/Daves_Insanity_Sauce

This is my .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^product/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+) /product?&cat=$1&q=$2&page=$3&prod=$4&prodName=$5

The first part allows me to go without the .php in the filenane.

This currently gives me error 500 when I try and go to: http://www.chillisource.co.uk/product/Grocery/Daves%20Gourmet/1/B0000DID5R/Daves_Insanity_Sauce

I've read up on mod_rewrite but I'm a novice programmer and finding it hard going. I don't get how $1, $2, $3, $4, $5 which are the variables in my url get transfered to the other side, is ([a-zA-Z0-9]+) correct?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsjq6977 2011-11-08 15:58
    关注

    $1 corresponds to your first set of parenthesis in the first part of your rewrite rule. $2 to the second set of parenthesis and so on.

    Daves%20Gourmet contains a space character which is not accounted for in your 2nd pattern i.e. things between 2nd set of parenthesis. Again you are not taking care of _ in the url.

    I would try this

    RewriteRule ^product/([a-zA-Z]+)/([\sa-zA-Z0-9]+)/([0-9]+)/([A-Z0-9]+)/([a-zA-Z_]+) /product?&cat=$1&q=$2&page=$3&prod=$4&prodName=$5
    

    Assuming your page parameter is always an integer and your 4th parameter contains only upper case letters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画