1

I have a site in localhost/gallery/ folder. I want to make this: - when I write localhost/gallery/12 real addres whic is open to be localhost/gallery/index.php?url=12 I tried to made it but unfortunately it doesn't work. Return error 310 (ERR_TOO_MANY_REDIRECTS).

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]
4

1 回答 1

1

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

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

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

试试这个:

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]

请注意,我在无限循环规则之前添加了一个重写条件,以阻止已经以 /gallery 开头的 URL 激活该重定向。

另外,我已经修改了文件规则,http://localhost/gallery/foobar以便http://localhost/gallery/index.php?url=foobar按照您的描述正确重写。请注意,/?此模式中的 the 为您提供了一个可选的尾部斜杠,因此http://localhost/gallery/12两者http://localhost/gallery/12/都可以工作。如果您不想要后者,请/?从模式中删除。

于 2012-06-19T15:10:13.623 回答