micobg
micobg

Reputation: 1342

How? (.htaccess redirect 301)

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]

Upvotes: 1

Views: 984

Answers (1)

daiscog
daiscog

Reputation: 12057

The following line actually forces an EXTERNAL redirect:

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

So that a request to http://localhost/foobar will result in a 301 response telling the client to request http://localhost/gallery/foobar. The client now makes a new request to http://localhost/gallery/foobar, which will now result in a 301 response to http://localhost/gallery/gallery/foobar, and so on.

Try this instead:

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]

Note that I've added a rewrite condition before your infinitely-looping rule to stop URLs that already begin with /gallery from activating that redirection.

Also, I've amended the file rule to properly rewrite http://localhost/gallery/foobar to http://localhost/gallery/index.php?url=foobar as you described. Note that the /? in this pattern gives you an optional trailing slash, so that http://localhost/gallery/12 and http://localhost/gallery/12/ both work. If you don't want the latter, remove the /? from the pattern.

Upvotes: 1

Related Questions