Argh, more .htaccess confusion!

Soldato
Joined
26 Aug 2006
Posts
9,726
Location
62.156684,-49.781113
There's so many things that can be done with .htaccess, I'm confused. All I want to do in this instance is redirect http://example.com/* to http://example.com to stop people getting to the hierarchy temporarily.

Currently got:

Code:
RewriteEngine On
RewriteRule (.*) http://example.com/$1 [R=301]

However, Firefox complains of infinite loops. How can I change it so that it only happens for sub-addresses?
 
RewriteEngine On
RewriteRule .+ http://example.com/ [R=302]

I haven't got regexbuddy up but am sure it's + that means "one or more times". I'm never confident with regular expressions without that application. Also I didn't test the above, so if it doesn't work (well even if it does) you should read on;

The reason the infinite loop is happening is because (.*) means "zero or more times". Also, the stuff inside the brackets (in your case, everything after example.com/) goes into $1, but with $1, you're just sticking that back on the url (?!) so basically you're going;

http://example.com/zebra --> matches (.*) - "zebra" goes into $1, so redirect to
http://example.com/zebra --> matches (.*) - "zebra" goes into $1, so redirect to
http://example.com/zebra --> matches (.*) - "zebra" goes into $1, so redirect to
... and on and on forever

but a plus means at least one character, and don't put $1 on the end of the new url or you're just taking them back to where the wanted to go anyway, so ...

http://example.com/zebra --> matches .+, so redirect
http://example.com --> doesn't match any rules (there are zero characters after the /, ".+" means at least one of any character), so do nothing

Plus, you should use a 302 redirect (temporary) not a 301 (permanent).
 
RewriteEngine On
RewriteRule .+ http://example.com/ [R=302]

I haven't got regexbuddy up but am sure it's + that means "one or more times". I'm never confident with regular expressions without that application. Also I didn't test the above, so if it doesn't work (well even if it does) you should read on;

The reason the infinite loop is happening is because (.*) means "zero or more times". Also, the stuff inside the brackets (in your case, everything after example.com/) goes into $1, but with $1, you're just sticking that back on the url (?!) so basically you're going;

Plus, you should use a 302 redirect (temporary) not a 301 (permanent).

That didn't work, but I see where you're coming from. It's been a while since I did any RegEx stuff, maybe that's where I should be reading. It still seems to be getting in a loop for the main domain, fixed by the below - seems there is still an infinite loop chance.

I use this code to do it

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

Think it's working. I've used a 302 here as said above, but I'm guessing this is ignoring the root request?

EDIT: Cancel that, it didn't work.
 
Last edited:
Here, this works - only allow people to request example.com/:

RewriteCond %{REQUEST_URI} !/
RewriteRule .* http://example.com/ [R=302,L]

I would've thought .+ would work, but apparently when requesting x.com/, it's matching the RewriteRule against the name of the directorys default index page - not against the blank string I was expecting. So it just gave yet another infinite loop :o
 
Last edited:
This is all:

Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/
RewriteRule .* http://jamiebarron.co.uk/ [R=302,L]
 
Serious wierdness, I'm certain I tested that last night. Just tested it again and, indeed, it doesn't work.

I'm making myself out to be a newbie. Web development is my job, honest! :o

This definitely works, I just tried it:

Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteRule .* http://jamiebarron.co.uk/ [R=302,L]
 
Right, I still don't get this. I'm now more understanding of what's doing what...

I've taken the rules off, and printed REQUEST_URI out on my page, it comes out as /

Why then, when I apply the rule:

Code:
RewriteCond %{REQUEST_URI} !^/$

It enters the condition?!
 
I have no idea mate, that's what confused me. It works on both my servers (anyone else care to check it out?!) one being a live CentOS/DirectAdmin install and the other a simple Ubuntu LAMP setup. Thus I gracefully disappeared from the thread :p

Maybe matching anything that's a forward slash followed by something?

RewriteCond %{REQUEST_URI} ^/.+$
 
You can use robots.txt:
User-agent: *
Disallow: /

You can also turn off directory indexes (indices?) by using the following in a .htaccess
Options -Indexes

If you want them to remain there but invisible/hidden, could you just move them to a sub-directory, which you can then password protect. Call it "old stuff" or something? Sorry if i've got the wrong end of the stick. :o
 
Why doesn't it quote code?! :confused:

You can use robots.txt:

Just a file called "robots.txt" in the root? So Google and such would see the 'holding' page at the root, but nothing else? I've got directories such as http://jamiebarron.co.uk/photos/ existing that occasionally appear from searches, but want them hidden.

You can also turn off directory indexes (indices?) by using the following in a .htaccess

Same effect as above?

If you want them to remain there but invisible/hidden, could you just move them to a sub-directory, which you can then password protect. Call it "old stuff" or something? Sorry if i've got the wrong end of the stick. :o

I'd considered that, wondered how to protect them though. Guessing this is another wonder of .htaccess?
 
I'd considered that, wondered how to protect them though. Guessing this is another wonder of .htaccess?

Yeah it's a wonder of .htaccess and his buddy, .htpasswd! :p

cPanel can generate these for you though, so if you go to your cPanel (which I assume you have) go to Password Protection, it'll set up password protection on a directory for you. :)

robots.txt just stops search engines from crawling and indexing your site... using just the robots.txt on its own, normal people would still abe able to access the files. :)
 
Back
Top Bottom