.htaccess - 将旧网址重定向到新网址

时间:2016-06-16 14:30:31

标签: .htaccess redirect mod-rewrite


我想将旧网址重定向到新网址:

www.bouda66.cz/?q=cs/tips_for_trips 转到 www.bouda66.cz
www.bouda66.cz/?q=cs/price_list 转到 www.bouda66.cz
www.bouda66.cz/?q=pl/price_list www.bouda66.cz/pl
www.bouda66.cz/?q=pl/tips_for_trips 转到 www.bouda66.cz/pl
...

我试过(但没有运气):
Redirect 301 /?q=cs/tips_for_trips http://www.bouda66.cz
RewriteRule ^/?q=cs/tips_for_trips$ http://www.bouda66.cz? [L,R=301]
RewriteRule ^?q=cs/.*$ http://www.bouda66.cz [L,R=301]

这是我的完整.htaccess

RewriteEngine on    

# Add www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]    

# If it is nested within a directory
RewriteBase /               

# Remove from url - index.php?page=
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^/?index\.php$ http://%{HTTP_HOST}/%1? [L,R=301]    

# Redirect for older pages
#Redirect 301 /?q=cs/tips_for_trips http://www.bouda66.cz
#RewriteRule ^/?q=cs/tips_for_trips$ http://www.bouda66.cz? [L,R=301]
#RewriteRule ^?q=cs/.*$ http://www.bouda66.cz [L,R=301] - error 500    

# Ignore all real directors
RewriteCond %{REQUEST_FILENAME} !-d
# Ignore all real files (php scripts, images, styles)
RewriteCond %{REQUEST_FILENAME} !-f
# Urls to exclude
RewriteCond %{REQUEST_URI} !^/sitemap.xml$
# everything else will generate index.php
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]    

DirectorySlash Off
你能帮助我吗? 由于
P.

1 个答案:

答案 0 :(得分:0)

如果要根据查询字符串重写URL,则需要使用RewriteCond。使用RewriteRule时,查询字符串不是其匹配的一部分。这就是您需要使用RewriteCond

的原因

这会检查查询字符串是否包含q = cs / {anything},然后检查其后的任何内容。它将重定向到www.example.com。

RewriteCond %{QUERY_STRING} (^|&)q=cs/(.*)($|&)
RewriteRule (.*) http://www.example.com? [R=301,L]

这会检查查询字符串是否包含q = pl / {anything},然后检查其后的任何内容。它将重定向到www.example.com/pl。

RewriteCond %{QUERY_STRING} (^|&)q=pl/(.*)($|&)
RewriteRule (.*) http://www.example.com/pl? [R=301,L]

您可以在http://htaccess.mwl.be/

上测试.htaccess规则