创建多个htaccess重写

时间:2012-07-15 21:27:10

标签: php .htaccess mod-rewrite

我目前有一个.htaccess文件,将dyhamb.com/episode.php?episode=1重写为dyhamb.com/1。我还希望将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]

1 个答案:

答案 0 :(得分:0)

您需要将2分开并复制您拥有的条件集。条件仅适用于紧随其后的规则:

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,并且我添加了在博客之后反向引用匹配(.*),因为您希望能够在其插入目标中的blog=查询字符串后访问该ID。此外,博客的外部重定向错过了ID之前的/blog/