apache htaccess 重写 url 以提高 seo 友好性

发布于 2024-12-14 07:50:50 字数 1102 浏览 0 评论 0原文

我正在尝试重写我的网址以提高搜索引擎优化友好性,但目前失败了。你能指出我哪里可能出错吗?

我正在尝试重写此网址:

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

对此:

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

这是我的 .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

第一部分允许我在文件名中不包含 .php。

当我尝试访问时,当前会出现错误 500: http://www.chillisource.co.uk/product/Grocery/Daves%20Gourmet/1/B0000DID5R/Daves_Insanity_Sauce

我已经阅读了 mod_rewrite 但我是一名新手程序员,发现很难。我不明白我的网址中的变量 $1、$2、$3、$4、$5 如何传输到另一端,([a-zA-Z0-9]+) 正确吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-12-21 07:50:50

$1 对应于重写规则第一部分中的第一组括号。 $2 到第二组括号,依此类推。

Daves%20Gourmet 包含一个空格字符,该字符未在您的第二个模式中考虑,即第二组括号之间的内容。同样,您没有处理网址中的 _

我会尝试这个

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

假设您的页面参数始终是整数并且您的第四个参数仅包含大写字母。

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文