301 重定向至动态 URL

301 重定向至动态 URL

过去几天我一直在研究动态 URL 的 301 重定向,确实很费劲。

我希望重定向example.com/?feed=podcastexample.com/feed/podcast

这是我当前的 .htaccess 文件:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^feed$ "http\:\/\/example\.com\/feed\/podcast" [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

我正在尝试为动态 URL 添加重定向。我已经能够让它工作:

RewriteCond %{QUERY_STRING} ^pizza$
RewriteRule ^$ http://www.example.com/feed/podcast? [R=301,L]

但我无法让它发挥作用:

RewriteCond %{QUERY_STRING} ^feed=podcast$
RewriteRule ^$ http://www.example.com/feed/podcast? [R=301,L]

最终重定向回 example.com

任何帮助将非常感激!

答案1

这对我有用:

Options All -Indexes

ErrorDocument 403 /Forbidden

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{QUERY_STRING} ^feed=podcast$
RewriteRule ^$ http://www.example.com/feed/podcast? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

起初我以为你必须用反斜杠来转义等号(“=”)(这确实有效,但如上所示,重定向也会起作用。

C:\Apache24\htdocs>curl -s --verbose http://localhost/?feed=podcast -o null
* Hostname was NOT found in DNS cache
*   Trying ::1...
* Connected to localhost (::1) port 80 (#0)
> GET /?feed=podcast HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 27 Aug 2015 21:43:43 GMT
* Server Apache/2.4.10 (Win64) PHP/5.6.4 is not blacklisted
< Server: Apache/2.4.10 (Win64) PHP/5.6.4
< Location: http://www.example.com/feed/podcast
< Content-Length: 243
< Content-Type: text/html; charset=iso-8859-1
<
{ [data not shown]
* Connection #0 to host localhost left intact

相关内容