.htaccess

He has: 44 posts

Joined: Dec 2003

I've been having alot of trouble with .htaccess at the moment. I have set up a site which I want to redirect any directories, apart from the ones i specify, to a file.

For example if someone was to type in site.com/hfhfrh it would redirect to a file but if they went to a directory i specify it would let them there (for example a folder called images).

How would I do this. I've tried Mod Rewrite but i can only make it redirect every directory even the ones i want not to redirect. Also when it redirects it redirects as if the file people are redirected to is in the directory they typed in. For example, the file might be in the root directory but if they went to a directory like 'spoons' the .htaccess file would redirect but as if the file was in the spoons folder.

Hopefully I explained well. If not i will try to explain more.

Busy's picture

He has: 6,151 posts

Joined: May 2001

you could use mod rewrite so that no slash went to file and slash went to folder
site.com/hfhfrh <-- file
site.com/hfhfrh/ <-- folder

easier way is not to worry about rewriting the folders and just do the files, gets a bit tricky when you working at different levels thou.

If you give us a bit of code it might be easier to tune for you

They have: 161 posts

Joined: Jan 2005

Can you tell us exactly what you want it to do? (IE: Make a .php?asd987a&hdasfu file into blah-blah.html file)

They have: 2 posts

Joined: Aug 2005

Why not use a 404 error page that redirects to the file you're talking about (if i'm correct on what you're trying to do)?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

From the way I have ready your post, I agree the 404 redirect would be what you want.

Now if you still want to have a different "404 error" for files off of your main site, you could do some checking to see what the really requested.

-Greg

They have: 2 posts

Joined: Aug 2005

Actually, it appears (another theory) you would want to block Hot-Linking and the execution of the files within the folder you are talking about. If you'd provide us with the exact scenario of what you're trying to do, it'd help.

He has: 44 posts

Joined: Dec 2003

Thanks for the help but after a good few hours researching regular expressions I managed to figure out what to do. I needed it to make this: http://www.zurl.net

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

yes, that is easy, we do that for affiliate links, do domain.com/textid and then let that redirect to the numeric one, so people could use more memorable urls.

We just set up the 404 page to check against a database (after a few checks to eliminate obvious not used ones). If it matched, it did a redirect to the other site after sending out the right headers. Otherwise it send the 404 headers and gave the error page.

-Greg

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Does anyone know of a good guide to all the different things you can do with .htaccess? Clean URLs, redirecting, error pages, interpreting html files as php, etc? I'm looking to do a bit more research into them and would love it if I could find one site with all the info I need.

Busy's picture

He has: 6,151 posts

Joined: May 2001

I recently lost all my bookmarks but there is a site with the name htaccess or similar, was a while ago but believe that is good and also has links. Like most topics, not any one site lists everything. Also have to remember not all work on all servers with a little tweaking.

He has: 4 posts

Joined: Jan 2006

This is a great article all about .htaccess file.

http://www.javascriptkit.com/howto/htaccess.shtml

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Nice link. Thanks, theramen!

I also found this page, which gives more info on transparently rewriting URLs:

http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.