Welcome to WebmasterWorld Guest from 18.206.194.83

Forum Moderators: phranque

Message Too Old, No Replies

How to redirect a folder to a file using .htaccess?

Any ideas why this code doesn't work?

     
8:18 am on Sep 8, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 11, 2003
posts:84
votes: 0


Hi

I have done a search on the forum and can't really seem to find the solution to my problem.

I am looking to direct a folder to a file, so when someone tries to read a page from this folder they are directed to this other page. I currently have tried :-

Redirect /folder/ http://www.example.com/file.html

RedirectPermanent /folder/ http://www.example.com/file.html

This does not appear to work. In both instances I am getting a 404 error saying http://www.example.com/file.htmlsomefile.html does not exist (where somefile.html was the file you typed in originally).

The code does load file.html but I can I get it to delete the bit afterwards so it works properly?

Many thanks to anyone who can help!

[edited by: bill at 4:36 am (utc) on Jan 27, 2017]
[edit reason] use EXAMPLE.COM to prevent auto-linking [/edit]

12:54 pm on Sept 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


bondjamesbond,

Redirect uses prefix matching. If the prefix matches, then the redirect is invoked, but anything following the prefix is taken from the requested URL and added to the substitution. This is precisely what you are seeing.

Use RedirectMatch [httpd.apache.org] in mod_alias [httpd.apache.org], or use mod_rewrite [httpd.apache.org] whenever you need to "strip off" any part of a URL, or to change that part's position in the new URL.

Jim

2:22 pm on Sept 8, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 11, 2003
posts:84
votes: 0


Many thanks jim for pointing me in direction of those documents they are most useful. I am very new to using .htaccess and are not very well acquaitained with unix and commands .htaccess uses.

I see this code

RedirectMatch (.*)\.gif$ [anotherserver.com$1.jpg...]

This replaces like-named gifs to jpgs. I do not have a clue as to the syntax needed to create a solution to my problem. How could I use RedirectMatch to match all files in a folder redirecting to a file?

I understand how redirect works but when it comes using (.*$ etc like as wildcards I have no idea! Can you provide any assistance?

2:35 pm on Sept 8, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 11, 2003
posts:84
votes: 0


Just looking around and I have discovered this :-

RedirectMatch /somefolder\.html$ [anotherserver.com...]
Would this do the job? My understanding of this is that any time it matches a .html file in the folder "somefolder" it redirects the file to file.html.

I can't test it until a bit later on, so I am going to give it a go to see if it works.

3:44 pm on Sept 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


> How could I use RedirectMatch to match all files in a folder redirecting to a file?

Pretty much exactly what you had before, changing only the directive itself, and using regular-expressions syntax for the pattern:


RedirectMatch 301 ^/folder/ http://www.mydomain.co.uk/file.html

For purposes of testing, remember that you can change "^/folder/" to anything you like - this path does not have to exist at all, since it will be redirected. So, you can test with a fictitious "/folder" so as not to disturb your site while doing it.

> ...when it comes using (.*$ etc like as wildcards I have no idea.

Here's a short Regular Expressions tutorial [etext.lib.virginia.edu].

Jim

5:52 pm on Sept 8, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 11, 2003
posts:84
votes: 0


Many thanks Jim that works perfectly.

Thanks also for that tutorial its really detailed and has helped me understand a bit more about regular expressions. Now I know a little bit more about them I can see where I was going wrong before. I just needed the solution to find out what the problem was :D

5:56 pm on Sept 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 21, 2003
posts:34
votes: 0


Cant you just set the default document for the folder to be the file you want them to goto?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members