【问题标题】:RewriteRule for SEO url'sSEO url 的 RewriteRule
【发布时间】:2016-08-08 21:04:08
【问题描述】:

我想使用 RewriteRule,但似乎无法掌握它。

这些是网站的层次:

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/' 之前的目录应该是很酷的 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 错误之后,我终于要问了。

【问题讨论】:

  • 只用一个路由库,有很多选择,而且都比你自己的diy版本更丰富、更健壮:google.co.uk/…
  • 我知道!问题是我无法让它工作。

标签: php regex .htaccess mod-rewrite


【解决方案1】:

您的正则表达式模式与给定的 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]

【讨论】:

  • 我想你刚刚成就了我的一年@anubhava!让我在这里再测试一些。
  • 这太棒了!你成功了!我还可以问一下应该如何通过说更多变量来完成?或者可能是无限变量?
  • 抱歉没有无限变幻的解法。对于非常复杂的情况,可以考虑使用Front Controller,但是对于添加 1 或 2 个新变量,可以使用上述重写规则来解决。
猜你喜欢
  • 1970-01-01
  • 2014-01-06
  • 2013-03-28
  • 1970-01-01
  • 2012-02-26
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多