3

I'm using .htaccess to redirect

http://www.example.com/foo/

to

http://www.example.com/foo/bar

This is my code:

redirect 301 /foo/ http://www.example.com/foo/bar

However this produces a feedback loop, something like

http://www.example.com/foo/barbarbarbarbarbar etc.

I've tried placing delimiters around it:

redirect 301 ^/foo/$ http://www.example.com/foo/bar

but then the redirect simply doesn't take place. I'm probably missing some very simple point of syntax. Any ideas? Thanks.

EDIT

Here's my (almost) full .htaccess file:

RewriteEngine On
RewriteBase /

# Canonical is www version
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
GluePear
  • 7,244
  • 20
  • 67
  • 120

4 Answers4

1

Have your rules like this:

RewriteEngine On
RewriteBase /

# Canonical is www version
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]

#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule !^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^foo/?$ /foo/bar [L,NC,R=301]

# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

make sure to test this in a new browser or clear your browser cache before testing.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

For your information, it really depends on your hosting provider. It may be behind a Load Balancer and you don't have the proper env var set (like HTTPS and others...).

In my case (Infomaniak), nothing actually worked and I got infinite redirect loop.

The right way to do this for Infomaniak is actually explained in their support site:

RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]

So, always check with your hosting provider. Hopefully they have an article explaining how to do this. Otherwise, just ask the support.

Indigo
  • 745
  • 5
  • 16
0

It depends on what is the resource that you want to expose with your redirect. Is it a file or a directory.

if you would expose a directory under bar you should try :

RedirectMatch 301 ^/foo/ http://www.example.com/foo/bar/

if you would redirect to a file you should use this syntax :

redirect 301 /foo http://www.example.com/foo/bar

you can see this post for more informations

Tony
  • 482
  • 4
  • 13
0

Instead of using redirect, you can use passthrough.

RewriteRule ^/foo$ /foo/bar [PT]
Sujith Mohan
  • 181
  • 1
  • 7