GoDaddy: RewriteCond in .htaccess not working with "beginning of the line" (^)

GoDaddy: RewriteCond in .htaccess not working with "beginning of the line" (^) - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, godaddy, , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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.


Solution :

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.



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.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect