Feodoran
Feodoran

Reputation: 1822

.htaccess redirct causes loop

I want to make some redirect like this:

Redirect 301 /foo /foo/bar/index.html

Problem is, it creates a loop.

Intuitively I would expect this redirect would match /foo only and not /foo.*. But apparently things work differently. How can I fix this?

Upvotes: 1

Views: 28

Answers (2)

Tuğberk Kaan Duman
Tuğberk Kaan Duman

Reputation: 950

This is an example .htaccess file of mine.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect 301 /CPP001/ https://mydomainaddress.com/cpp/student-app/
Redirect 301 /DSE001/ https://mydomainaddress.com/cpp/examples/virtual.html
Redirect 301 /DSE002/ https://mydomainaddress.com/cpp/examples/overload.html
Redirect 301 /ANK001/ https://mydomainaddress.com/cpp/com267/
Redirect 301 /ANK002/ https://mydomainaddress.com/c-programming/com267HW2/
Redirect 301 /WEB001/ https://mydomainaddress.com/web-programming/examples/index.html
Redirect 301 /NET001/ https://mydomainaddress.com/pdf/IPControl.pdf
ErrorDocument 404 https://mydomainaddress.com/
ErrorDocument 403 https://mydomainaddress.com/

Try to change your Redirect 301 /foo /foo/bar/index.html to Redirect 301 /foo http://yourdomain.com/foo/bar/index.html

If nothing inside index.html is causing another redirection, it's highly unlikely a loop will happen again.

Upvotes: 0

anubhava
anubhava

Reputation: 785146

Use RedirectMatch instead of Redirect to be able to use regex for exact matching:

RedirectMatch 301 ^/foo/?$ /foo/bar/index.html

Make sure to use a new browser for testing this change.

Regex ^/foo/?$ will only match /foo or /foo/ but not /foo/anything.

Upvotes: 2

Related Questions