[英]Creating multiple htaccess rewrites
我目前有重寫.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]
您需要將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
,並且我添加了一個反向引用的匹配項(.*)
,因為您希望能夠訪問ID,然后將其插入目標中的blog=
查詢字符串中。 另外,博客的外部重定向在ID之前缺少/blog/
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.