4

I have .htaccess containing RewriteCond and RewriteRule.

It used to work just fine (it's been a long time since I last checked), but now it has stopped working. I have narrowed it down to the following example.

Suppose I want to redirect www.mydomain.com/foo but not when foo occurs anywhere else; so, for example, don't redirect if the URI is /abc/foo, /abcfoo, or /foobar.

Here is my current .htaccess file:

Options -Indexes +FollowSymLinks -Multiviews

RewriteEngine On

RewriteCond  %{REQUEST_URI}  ^/foo$
RewriteRule  .  /redirect.html  [last,redirect=temp]

I have tried changing ^/foo$ in various ways, but I can't get this to work.

The following all work as expected:

foo         Redirects wherever foo appears, e.g. /abcfoobar
foo$        Redirects whenever foo appears at the end, e.g. /abc/defoo
/foo        Redirects whenever /foo appears, e.g. /abc/foobar
/foo$       Redirects whenever /foo appears at the end, e.g. /abc/foo

The following always return 404 Not found, no matter what I put as the URI.

^foo
^/foo
^/foo$

I don't know what's going on.

Please help me fix this. In case it helps, I'm using GoDaddy hosting.

  • Just as a test to see what %{REQUEST_URI} actually contains, you could try something like: RewriteCond %{REQUEST_URI} !test RewriteRule . /test%{REQUEST_URI} [R=302,L] - What does it (try to) redirect to? Specifically, what is after "/test"? – MrWhite Feb 9 '15 at 19:48
  • @w3d I've just spotted your suggestion. If I'd seen it earlier, it would have helped me. I shall remember this, thank you! See my answer for the explanation. – Paddy Landau Feb 9 '15 at 20:20
4

I’m not sure what the problem is in your case (I tested your snippet and it works as expected), but maybe this works for you:

RewriteEngine On
RewriteRule ^foo$ /redirect.html [redirect=temp,last]

I’m not an .htaccess expert, but I guess this would even be faster, because the RewriteRule pattern gets processed before RewriteCond.

  • That works, thanks. The problem was that I wanted to use RewriteRul in conjunction with RewriteCond (for more complex conditions), but as a workaround, I shall use your suggestion, amended as required. As you can confirm that yours is working correctly, I shall get hold of GoDaddy to find out what's happening on their end. Thank you. – Paddy Landau Feb 9 '15 at 18:09
  • I have discovered the problem, which seems to apply specifically to GoDaddy. I've added an answer to explain it. – Paddy Landau Feb 9 '15 at 20:17
3

I have just discovered the solution! This seems to be a GoDaddy special.

Domains in GoDaddy are kept in subfolders; e.g. mydomain.com might be held in a subfolder called md.

Curiously, the start of %{REQUEST_URI} includes this string. Therefore, my RewriteCond should have read:

RewriteCond  %{REQUEST_URI}  ^/md/foo$

It is not needed if the string does not necessarily start at the beginning. This explains my strange results.

I have edited my title to show that this applies specifically to GoDaddy, in the hope that this helps someone else.

  • 1
    Interesting! So /md doesn't actually appear in the URL you type in the address bar? It sounds like GoDaddy is doing an internal subrequest to this folder (is REDIRECT_STATUS set)!? Your .htaccess file is presumably in this subfolder? Just curious... what GoDaddy hosting package is this? – MrWhite Feb 9 '15 at 20:54
  • Apparently GoDaddy has updated its hosting package, and I'm still on the older one. I don't know how to check the value of REDIRECT_STATUS. To confirm, /md does not appear in the URL, only in the %{REQUEST_URI} within .htaccess. I wonder if I upgrade to the newer GoDaddy package (which is a lengthy process) if it would still happen. – Paddy Landau Feb 10 '15 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.