homepage Welcome to WebmasterWorld Guest from 54.227.160.102
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect in .htaccess
lappert2001

5+ Year Member



 
Msg#: 4415683 posted 2:26 am on Feb 9, 2012 (gmt 0)

Running Apache 2.2

I have this structure:
mydomain.com/forum/board/index.html

The /forum directory is empty, so I wish to place a .htaccess file to redirect it to /forum/board/index.html.

I place the .htaccess file with the directive:
Redirect /forum /forum/board/index.html
the .htaccess file is 644 root:root (would that make a difference?)

The Redirect is not working. Firefox reports:

The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

I tried expanding the old and new to full URLs:
Redirect http://mydomain.com/forum http://mydomain.com/forum/board/index.html
... but that doesn't fix it.

I'd appreciate any help. Thanks.

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 2:49 am on Feb 9, 2012 (gmt 0)

When you use Redirect the original requested path is reappended. This new URL request still matches the rule and will be redirected again.

Don't use Redirect.

Use RewriteRule with the [R=301,L] flags.

Make sure the RegEx pattern matches "exactly" folder and not simply "begins" with or "contains" the folder name.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 4:22 am on Feb 9, 2012 (gmt 0)

Thanks for the reply. I'm not that familiar with Rewrite and the Redirect seemed simple and to the point.

Can you point to a Rewrite example that would work in this situation?

Thank you.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 5:00 am on Feb 9, 2012 (gmt 0)

Been reading and researching. Now trying:

RewriteEngine On
RewriteRule /forum/index.html /forum/board/index.html [R=301,L]

But it's still not working. One note. There is no /forum/index.html file, but it should redirect instead to /forum/board/index.html (which does exist). I hope that makes sense.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 9:45 am on Feb 9, 2012 (gmt 0)

Replace the leading / with ^ in the pattern.

Replace . with \. in the pattern.

Change /index.html to /(index\.html?)?$ in the pattern.

Add http://www.example.com to the front of the target URL.

Remove index.html from the target URL and be sure that you have a DirectoryIndex directive that lists index.html as one of the possibilities.

Be sure to read as much as you can about Regular Expressions as they come up time and time again in programming. Check out a tutorial on mod_rewrite and using RewriteRule.

Everything you can do with Redirect can be done with RewriteRule. RewriteRule can do a lot of stuff that Redirect cannot. You can safely forget that Redirect and RedirectMatch even exist. Just learn the RewriteRule stuff.

[edited by: g1smd at 9:49 am (utc) on Feb 9, 2012]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4415683 posted 9:47 am on Feb 9, 2012 (gmt 0)

It doesn't matter at all if the "pattern" really exists or not. It doesn't even matter if the "target" exists. But you're making several mistakes.

On the "pattern" side:

#1 Do not begin a pattern with / It will never match, unless you get a malformed request with //
#2 Use anchors in the pattern
#3 Allow for both / (final slash) and /index.html by saying /(index\.html)?$ at the end. On the "pattern" side you are dealing with actual user input; you might get either form.

On the "target" side:

#1 Include the full protocol and domain name
#2 Never use the form "/index.html". The canonical name of a directory ends in / alone.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 9:50 am on Feb 9, 2012 (gmt 0)

I think we're in agreement there!

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 2:51 pm on Feb 9, 2012 (gmt 0)

Thank you for the replies.

I now have:
DirectoryIndex index.html

RewriteEngine On
RewriteRule ^forum/(index\.html?)?$ [mydomain.com...] [R=301,L]

I also have the DirectoryIndex in a .htaccess file in the parent directory, but put another in.

Unfortunately it is still not working.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 4:28 pm on Feb 9, 2012 (gmt 0)

Use example.com in this forum to stop URL auto-linking.

The above code belongs in the root htaccess file.

If this code is in the /forum/ folder htaccess file you must omit forum/ from the rule pattern.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 4:48 pm on Feb 9, 2012 (gmt 0)

Hooray! Now it's working.

Sorry about the autolinking. I'll try to be careful about that.

I used your second suggestion, removing forum/ from the first parameter. The root .htaccess file is much more complex and has its own problems. We just moved from an old server (Debian Sarge) to Debian Squeeze and a lot of issues have cropped up. We're attacking them one at a time.

Thanks again for your assistance.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 4:50 pm on Feb 9, 2012 (gmt 0)

Oops, forgot to include the code as it is now (and working):

DirectoryIndex index.html

RewriteEngine On
RewriteRule ^(index\.html?)?$ http://example.com/forum/board/ [R=301,L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 6:27 pm on Feb 9, 2012 (gmt 0)

In general it is best to have all the code in the root folder as then you control the order of operation more easily.

There's one issue that you cannot fix by having your code in a folder.

User requests
www.example.com/forum/
Your root htaccess redirects the user to
example.com/forum/
Finally, your forum htaccess file redirects the user to
example.com/forum/board/

You should avoid this redirection chain.

You need the www to non-www redirect to be last in the chain, but you can't because the root htaccess is processed before the folder htaccess.

You could add a folder exclusion to the root htaccess www to non-www redirect and then duplicate the www to non-www redirect code in the htaccess file in the forum folder, but this amount of complexity is hard to maintain and likely to break something else.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 7:35 pm on Feb 9, 2012 (gmt 0)

We're not handling the www to non-www redirect via the .htaccess file.

Instead -- and we've done this for years - is to do it through a ServerAlias directive in each domain's vhost file. That works well. But also it seems the DNS A record does the same thing. Either way, it's not in the .htaccess file.

Again, thank you.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 7:56 pm on Feb 9, 2012 (gmt 0)

ServerAlias just makes the other hostname variant "active".
It doesn't redirect non-www requests to www as far as I know.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 8:17 am on Feb 10, 2012 (gmt 0)

Again, thanks.

It seems since we moved our server two weeks ago, as soon as one issue is discovered and fixed, a new one appears. This one was noticed yesterday, and to be honest, I don't know if it was created in the process of fixing the other issue. It seemed to appear at the same time. Apologies if this should be posted as a new thread - tell me and I'll do that.

In any case, it's still Apache 2.2. We have an instance of phpBB2 running (the issue above to /forum was our archived forum prior to phpBB2).

The structure is:

example.com/phpBB2/index.php. That still works fine.

But most of the links to the forum (on our site and from other sites) are simply example.com/phpBB2 (no trailing slash). This has never been a problem before. People would link to example.com/phpBB2 and be directed to example.com/phpBB2/index.php

But now it's not working and the result is a 403 Forbidden. There is no .htaccess file in this directory. But there is a .htaccess file in the root directory. I could post that code if it would help.

Any thoughts?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4415683 posted 8:51 am on Feb 10, 2012 (gmt 0)

People would link to example.com/phpBB2 and be directed to example.com/phpBB2/index.php

But now it's not working and the result is a 403 Forbidden.


Waitwaitwait. Didn't you just get through saying
DirectoryIndex index.html

?

If mod_dir is running-- which it certainly should be!-- a request for
example.com/directory

will first be redirected (301) to
example.com/directory/

and then to
example.com/directory/index.html

if it exists. If it doesn't exist, then we hand off to mod_autoindex and see whether autoindexing is enabled for /directory. If it isn't, the human user gets hit with a 403.

If the index file is not named index.html but is named something else, such as index.php, you have to say so. Otherwise it's as if it doesn't exist.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 9:29 am on Feb 10, 2012 (gmt 0)

According to example.com/phpinfo.php both mod_dir and mod_autoindex are running.

I added Directory Index to example.com/forum/.htaccess at the suggestion above. But that's a different problem nothing to do with this new problem.

However DirectoryIndex has been in the root .htaccess all along.

In this problem ... a user requesting example.com/phpBB2 is supposed to be redirected to example.com/phpBB2/index.php (not index.html). Until yesterday it seemed to work.

If named index.php, you say I have to say so. OK, where/how would I say that? And what troubles me is why it worked before and what caused it not to work.

Thank you.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 9:34 am on Feb 10, 2012 (gmt 0)

DirectoryIndex lists valid index filenames that Apache should search for when bare folder name with slash is requested.

DirectorySlash redirects requests to add the trailing slash when bare folder name is requested but without the required trailing slash.

[edited by: g1smd at 10:26 am (utc) on Feb 10, 2012]

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 10:00 am on Feb 10, 2012 (gmt 0)

So I just changed in the root .htaccess

from:
DirectoryIndex index.html
to:
DirectoryIndex index.html index.php

I did not add DirectorySlashes

... and it seems to work. While I wish I knew what caused this to occur (after working fine for many years), I'll just accept the good luck.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 10:09 am on Feb 10, 2012 (gmt 0)

Well I just discovered what made the problem occur.

Before moving to the new machine, there was no DirectoryIndex directive in the root .htaccess and things were fine. (I thought it had been tehre but I was mistaken)

So when I had the problem with /forum ... your suggestion above had me add: DirectoryIndex index.html

That caused it to be broken for example.com/phpBB2/

Now I've added: DirectoryIndex index.html index.php

And things are working again (at least for those two directories).

Again, thank you.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 10:16 am on Feb 10, 2012 (gmt 0)

If I remember right, if you add
DirectoryIndex you need to add DirectorySlash too.

I think this is one of the occasions where the Apache manual issues "dire warnings" about doing it any other way. Do read the manual. There's several directivies that are intricately tied up with each other and all need to work together.


Ah yes, it is [httpd.apache.org...]

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 10:30 am on Feb 10, 2012 (gmt 0)

Thanks.

I added it and things still work. Maybe it's just good practices.

BTW, it's DirectorySlash (singular), not DirectorySlashes (plural). I found that when the plural didn't show up in Google. :)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 10:57 am on Feb 10, 2012 (gmt 0)

Yeah, I always get that one wrong and have corrected my earlier post.

There's a security warning in the Apache manual for the case of using DirectoryIndex but omitting DirectorySlash.

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 12:53 pm on Feb 10, 2012 (gmt 0)

I looked for the warning in the Apache documentation, but didn't see it. But if it works in both, I'm OK with that.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 1:06 pm on Feb 10, 2012 (gmt 0)

Security Warning
Turning off the DirectorySlash trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.

[httpd.apache.org...]

lappert2001

5+ Year Member



 
Msg#: 4415683 posted 1:19 pm on Feb 10, 2012 (gmt 0)

Thanks

There actually is a case where I want a directory listing. I use an old log analyzer (Accesswatch) which creates a file for each day showing usage. The directory with the historical files I view as a directory listing. But the entire thing is behind a .htpasswd, so it's not public.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415683 posted 1:23 pm on Feb 10, 2012 (gmt 0)

Sure, you can set things to show directory listings and that's sometimes desireable.

What they are warning about is requesting
* /folder/index.php - shows contents of index.php file
* /folder/ - shows contents of index.php file
* /folder - lists all the files in the folder - potential security problem.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4415683 posted 9:09 pm on Feb 10, 2012 (gmt 0)

Apache says the DirectorySlash redirect is on by default, so there is no need for mass panic ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved