子文件夹查询字符串htaccess重定向

时间:2014-02-06 18:43:30

标签: .htaccess redirect query-string

我能找到的查询字符串重定向的所有内容都是关于这样的内容:

RewriteEngine on 
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]

这很好,但我正在处理一个看起来像这样的网址:

example.com/portfolio/?gal=16

如果我在没有“组合/”的情况下进行测试,它会正常工作,但我的旧网站上面已编入索引。关于如何使其与我的URL结构一起使用的任何提示?

编辑当前.htaccess内容

# 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

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# Redirects

Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us

#futile attempts at query string redirects omitted

感谢。

新编辑以包含@ anubhava的建议

也可以提供实际的.htaccess内容,因为该网站现已上线。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]

Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us

#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1

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

# END WordPress

# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21

1 个答案:

答案 0 :(得分:1)

您需要更改规则的顺序,并在WP规则之前保留301/302:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

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

# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]

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

# END WordPress