Php 重写搜索引擎优化url的规则';s

Php 重写搜索引擎优化url的规则';s,php,regex,.htaccess,mod-rewrite,Php,Regex,.htaccess,Mod Rewrite,我想使用重写规则,但似乎不太懂 以下是网站的各层: http://domain.com/artists/artist-name/1/2/3/ http://domain.com/fairytales/fairy-tale-one/1/2/3/ http://domain.com/horrorstories/horror-story-six/1/2/3/ 基本上,url中“/1/”前面的dir应该是很酷的SEO url名称 到目前为止,我明白了,应该是这样改写的 RewriteEngine On

我想使用重写规则,但似乎不太懂

以下是网站的各层:

http://domain.com/artists/artist-name/1/2/3/
http://domain.com/fairytales/fairy-tale-one/1/2/3/
http://domain.com/horrorstories/horror-story-six/1/2/3/
基本上,url中“/1/”前面的dir应该是很酷的SEO url名称

到目前为止,我明白了,应该是这样改写的

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/artists/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?artist_id=$1&cat_id=$2&style_id=$3
RewriteRule ^/fairytales/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?story_id=$1&cat_id=$2&style_id=$3
RewriteRule ^/horrorstories/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?story_id=$1&cat_id=$2&style_id=$3
.htaccess在我使用的CentOS服务器上与Apache 2.4.23配合使用


在经历了500个错误和404个错误之后,我终于要问了。

您的正则表达式模式与给定的URL不匹配。试试这些规则:

RewriteEngine On

RewriteRule ^/?artists/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?artist_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]

RewriteRule ^/?fairytales/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?story_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]

RewriteRule ^/?horrorstories/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?story_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]

只要使用一个路由库,就有很多可供选择,它们都将比您自己的diy版本功能更丰富、更健壮:我知道!问题是我不能让它工作。我想你刚刚让我的一年@anubhava!让我在这里再测试一下。这太棒了!你成功了!我还可以问一下,如果有更多的变量,应该怎么做?或者可能是无限变量?对不起,没有无限变量的神奇解决方案。对于非常复杂的情况考虑使用,但是对于添加1个或2个新变量,可以使用上述改写规则来计算。