1

我目前有一个 .htaccess 文件重写dyhamb.com/episode.php?episode=1dyhamb.com/1. 我还想要另一个重写dyhamb.com/blogpost.php?bp=1dyhamb.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]
4

1 回答 1

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。此外,博客的外部重定向缺少/blog/之前的 ID。

于 2012-07-16T15:50:06.217 回答