如何使用.htaccess重定向具有#字符的url

时间:2011-04-29 15:59:32

标签: .htaccess mod-rewrite redirect

我已经看了好几天了,即使工作中的IT人员试图解决这个问题,但我们并没有走得太远。

我有一个网址my.website.com/index.php#!lightBox[gallery]/0/,我想将其重定向到my.website.com。我只用index.php可以做得很好,但这是额外的部分不起作用。我试过了:

RewriteCond %{HTTP_HOST} ^my.website.com$ [NC]
RewriteRule (.*) http://my.website.com/index.php#!lightBox[gallery]/0/? [R=301,L]

和cpanel生成了以下重定向:

RewriteCond %{HTTP_HOST} ^my.website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.my.website.com$
RewriteRule ^/?$ "http\:\/\/my\.website\.com\/\#\!lightBox\[gallery\]\/0\/" [R=301,L]

输出my.website.com/%23!lightBox[gallery]/0/作为网址 - 正如您所见,哈希未被处理。

如果有人有任何想法,我很乐意听到!

谢谢:)

1 个答案:

答案 0 :(得分:0)

你的规则如下:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com$ [NC]
RewriteRule ^/?$ http://my.website.com/index.php#!lightBox[gallery]/0/ [R=301,L,NE]

您需要在RewriteCond中转义点.,但不需要转义RHS上的任何特殊字符。重要的是,您需要使用标记NE来避免生成的URI。