我目前有一个将dyhamb.com/episode.php?episode=1
重写为dyhamb.com/1
的.htaccess文件。我也想要另一个将dyhamb.com/blogpost.php?bp=1
重写为dyhamb.com/blog/1
的工具。
我已经为剧集重写设置了代码,但是当我去添加博客重写时,我似乎不能让它工作。我该如何修改以下代码来实现这一点呢?
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^dyhamb\.com$
RewriteRule ^(.*) http://dyhamb.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
RewriteRule ^/blog$ /blogpost.php?blog=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blogpost\.php\?blog=(\d+) [NC]
RewriteRule ^ %1? [R=301,L]
发布于 2012-07-16 23:50:06
您需要将这两个条件分开,并复制您已有的条件集。这些条件仅适用于紧随其后的规则:
RewriteCond <something>
RewriteCond <something-else>
# those 2 conditions only apply to this rule:
RewriteRule <match> <target>
# This rule has no conditions
RewriteRule <match2> <target2>
所以你想让你的htaccess看起来像这样:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^dyhamb\.com$
RewriteRule ^(.*) http://dyhamb.com/$1 [R=301,L]
# Setup conditions for internal rewrite of episode.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for episode.php
RewriteRule ^(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
# Setup conditions for internal rewrite of blopost.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for blogpost.php
RewriteRule ^blog/(.*)$ /blogpost.php?blog=$1 [L,QSA]
# External redirect for episodes
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L]
# External redirect for blog
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blogpost\.php\?blog=(\d+) [NC]
RewriteRule ^ /blog/%1? [R=301,L]
请注意,需要对您的博客规则进行一些更改。如果这些规则将位于.htaccess文件中,则在重写引擎处理它之前,URI中的前导斜杠将被去掉,因此表达式^/blog
需要为^blog
,并且我在博客后面添加了一个反向引用的匹配(.*)
,因为您希望能够访问它后面的ID以插入到目标中的blog=
查询字符串中。此外,博客的外部重定向在ID之前缺少/blog/
。
https://stackoverflow.com/questions/11495746
复制相似问题