2012-06-19 52 views
1

我在localhost/gallery /文件夹中有一个网站。我想这样做: - 当我写本地主机/画廊/ 12真正的地址是开放的本地主机/画廊/ index.php?url = 12 我试图做到了,但不幸的是它不工作。返回错误310(ERR_TOO_MANY_REDIRECTS)。如何? (.htaccess重定向301)

AddDefaultCharset UTF-8 

Options +FollowSymlinks -Indexes 
RewriteEngine On 
RewriteCond %{THE_REQUEST} /index\.(php|html)\ HTTP/ 
RewriteRule ^index\.(php|html)$/[R=301,L] 
RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L] 

RewriteBase/

RewriteRule ^([^.]+)$ index.php?url=$1 [L,NC,QSA] 
+0

您能否让我知道我发布的解决方案是否解决了您的问题? – megaflop

回答

1

以下行实际上将强制外部重定向:

RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L] 

因此,为http://localhost/foobar的请求将导致301响应告诉客户端请求http://localhost/gallery/foobar。现在,客户端发出新的请求http://localhost/gallery/foobar,该请求现在将导致对http://localhost/gallery/gallery/foobar的301响应,依此类推。

试试这个:

AddDefaultCharset UTF-8 

Options +FollowSymlinks -Indexes 
RewriteEngine On 
RewriteCond %{THE_REQUEST} /index\.(php|html)\ HTTP/ 
RewriteRule ^index\.(php|html)$/[R=301,L] 

RewriteCond %{REQUEST_URI} !^/gallery/ 
RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L] 

RewriteBase/
RewriteRule ^gallery/([^.]+)/?$ gallery/index.php?url=$1 [NC,QSA,L] 

注意你的无限套住的规则之前,我已经添加了一个改写条件停止已经与/画廊开始从激活该重定向URL。

此外,我已经修改了文件规则,以正确地将http://localhost/gallery/foobar重写为http://localhost/gallery/index.php?url=foobar,正如您所描述的那样。请注意,此模式中的/?为您提供了一个可选的结尾斜杠,因此http://localhost/gallery/12http://localhost/gallery/12/都可以使用。如果你不想要后者,从模式中删除/?