homepage Welcome to WebmasterWorld Guest from 107.20.91.81
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect folder directory
TheKiller




msg:4351617
 2:53 pm on Aug 15, 2011 (gmt 0)


So i have had my forum at the fallowing url:
http://www.example.com/Forums/
I wished to re-name my folder into /forums/.
I have used the script bellow to make both folder paths available.

RewriteRule ^Forums/(.*) forums/$1

http://www.example.com/Forums/viewforum.php?f=2
and
http://www.example.com/forums/viewforum.php?f=2
Show the Same contents.

I would like that when i access:
http://www.example.com/Forums/viewforum.php?f=2
It will redirect to:
http://www.example.com/forums/viewforum.php?f=2

I hope i explained well :)

 

g1smd




msg:4351634
 3:27 pm on Aug 15, 2011 (gmt 0)

Lets see your RewriteRule code for a 301 redirect. There's thousands of prior examples, indeed there was a post about 5 hours ago with a simple example to follow. You'll need the [R=301,L] flags to make it work.

However, the first step is that you will need to amend the links on the pages of your site to point to the new URLs. URLs are defined in links. You need to link to the URL that you want users to see and use.

Is the physical folder name inside the server changing too, or do you simply want to use new URLs out on the web?

lucy24




msg:4351657
 4:13 pm on Aug 15, 2011 (gmt 0)

Are you asking about the query string (the part after the question mark)? You don't need to do anything about that. Unless you give other instructions, mod_rewrite will stash any existing query in a safe place, do all its stuff, and then quietly reappend the query without change.

Second paragraph deleted because g1 types faster than I do.

TheKiller




msg:4351671
 4:36 pm on Aug 15, 2011 (gmt 0)

@g1smd, i have replaced :
RewriteRule ^Forums/(.*) forums/$1

With:
RewriteRule ^Forums/(.*) forums/$1 [R=301,L]

When i visit:
http://www.example.com/Forums/viewforum.php?f=2
It redirects me to:
http://www.example.com/home/user/domains/example.com/public_html/forums/viewtopic.php?f=2

Im not quite sure what you are asking.

Ill try to explain again.

I have had my forum into the /Forums/ folder.
I have renamed it to /forums/ And used " RewriteRule ^Forums/(.*) forums/$1 " To send the traffic from /Forums/ to /forums/

So, when you access /Forums/ or /forums/ you will see the same contents.
All fine.

I just want it, so when you visit /Forums/ you will be redirected to /forums/.

That should be happening in other pages inside the /forums/ path also.

lucy24




msg:4351820
 10:21 pm on Aug 15, 2011 (gmt 0)

Urk. Do you have a RewriteBase lurking in your htaccess somewhere? Most of that new address shouldn't even be publicly viewable. The home/user/domains/example.com/public_html/ part is physically part of the path, but it shouldn't be showing up for the human visitor.

TheKiller




msg:4352006
 1:54 pm on Aug 16, 2011 (gmt 0)

Hi Lucy, Idk what you mean by that "RewriteBase lurking".
I have just added only :

RewriteEngine On
RewriteRule ^Forums/(.*) forums/$1 [R]

And it gave the same problem.

The "Weird" Address isnt showing contents. just a 404 error if i remember right.

g1smd




msg:4352125
 5:33 pm on Aug 16, 2011 (gmt 0)

When an internal filepath is exposed as a new URL, this usually occurs because you have a redirect that takes place after the request has been internally rewritten.

It is likely that your rules are in the wrong order.

If you have a .htaccess file in a sub-folder, then the rules inside that file are processed after the rules in the .htaccess file in the root of the site.

If there's a rewrite in the root file and a redirect in the sub-folder file then that will cause the observed problem.

Also, be aware that [R] produces a 302 redirect and that for redirects the RewriteRule target should include the domain name. Every RewriteRule should include the [L] flag unless you know exactly why it should be omitted.

TheKiller




msg:4352126
 5:37 pm on Aug 16, 2011 (gmt 0)

Im sorry but i dont understand what exactly do you mean.
Well i understand. but i dont think theres anything wrong with it. If it will help i can give you the .htaccess files from my folders.

lucy24




msg:4352236
 10:56 pm on Aug 16, 2011 (gmt 0)

The "Weird" Address isnt showing contents. just a 404 error if i remember right.

Yes, that's what it should be doing. You can think of error pages such as 404 and 403 as a special kind of rewrite. The user's browser address bar shows the page they thought they were going to, while their screen shows the error that results from your server and/or their browser trying to take them there.

Try it this way: Where is your htaccess located? Ordinarily it should be in the same directory as your top-level index.html (the one users get taken to if they just enter your domain name) and other all-purpose files like robots.txt.

If it will help i can give you the .htaccess files from my folders.

Uh-oh, that looks like a plural to me. If you have more than one htaccess, where is each of them located? Ignore the ones that say nothing except "Options -Indexes" or "Options +Indexes"; those should have no effect on anything else.

All of this is assuming you are talking only about .htaccess. If you've got something further up like a config file, different rules kick in.

g1smd




msg:4352241
 11:18 pm on Aug 16, 2011 (gmt 0)

I'm sorry but I don't understand what exactly do you mean.
I don't think I can explain it any more simply, or clearly, than I have done so already.

If you're not already clear on the concept of what mod_rewrite does, and the various terms like redirect and rewrite and what they mean, then a thorough read of the Apache mod_rewrite manual is in order.

TheKiller




msg:4352350
 6:51 am on Aug 17, 2011 (gmt 0)

There is the main .htaccess file, where i put this code in. And there is the .htaccess file inside the forum path, coming with the forum software.

Im putting the code inside my /public_html/
If you're well with Apache. Could you please test the line on your server and see if it does what i want?
We can see how to fix it if its a problem on my side, or its just something wrong with th piece of line.

g1smd, i wasnt sure why you were saying it. i think i already did that. i got sure that there is NOTHING Else in my htaccess file, besides two lines specified.

lucy24




msg:4352358
 7:15 am on Aug 17, 2011 (gmt 0)

Im putting the code inside my /public_html/

Uh-oh. I haven't got one of those. (Is there some arcane reason why error logs give the actual path and access logs don't?) Mine goes

/home/{username}/example.com/

where {username} also branches out to /logs/ and /otherdomain.com/

Anyway, your primary htaccess should be inside of /example.com/ You must have one somewhere.

TheKiller




msg:4352371
 7:57 am on Aug 17, 2011 (gmt 0)

Yesssss Lucy, im putting the .htaccess file into the main folder, in the root. Comeone, you are making me feel like an idiot.

Im on Linux hosting and i have /public_html/ instead of /www/ or whatever....

I cant see anything wrong in my logs....

Wed Aug 17 03:49:00 2011] [error] [client 79.113.108.96] File does not exist: /home/risingde/domains/example.com/public_html/home
[Wed Aug 17 03:49:00 2011] [error] [client 79.113.108.96] File does not exist: /home/risingde/domains/example.com/public_html/404.shtml
[Wed Aug 17 03:49:14 2011] [error] [client 66.249.72.20] File does not exist: /home/risingde/domains/example.com/public_html/home
[Wed Aug 17 03:49:14 2011] [error] [client 66.249.72.20] File does not exist: /home/risingde/domains/example.com/public_html/404.shtml
[Wed Aug 17 03:50:49 2011] [error] [client 66.249.72.20] File does not exist: /home/risingde/domains/example.com/public_html/home
[Wed Aug 17 03:50:49 2011] [error] [client 66.249.72.20] File does not exist: /home/risingde/domains/example.com/public_html/404.shtml



The .htaccess file its on its place. The problem is in the code.

Have you tested this ?

RewriteEngine On
RewriteRule ^Forums/(.*) forums/$1 [R]

Test it and tell me if it works on your server.

g1smd




msg:4352372
 8:00 am on Aug 17, 2011 (gmt 0)

Redirects should have the domain name in the target as well as the [L] flag.

[R] on its own produces a 302 redirect.

Use the Live HTTP Headers extension for Firefox to see what is going on.

lucy24




msg:4352419
 11:50 am on Aug 17, 2011 (gmt 0)

[Wed Aug 17 03:50:49 2011] [error] [client 66.249.72.20] File does not exist: /home/risingde/domains/example.com/public_html/404.shtml

Do you get the impression your htaccess is laughing at you? :) For a while, I had the yandexbot actively asking for the 403 page in one directory. But that

/home/risingde/domains/example.com/public_html/home

is obviously wonky. That's why I asked about a RewriteBase.

I was going to say that since your directory structure is different from mine, it wouldn't be useful to try your exact wording. But I tried it in my under-construction site (where no harm is done if there's a 500-level calamity) and holy cow, I get the identical pattern: if I request

http://example.com/Forums/foobar.html

I get the 404 page at

http://example.com/home/{username}/example.com/forums/foobar.html

All for the want of a lowly slash: change the rewrite to

RewriteRule ^Forums/(.*) /forums/$1 [R]

and it plays nice. Apparently my host supplies the domain name; results are identical if I do the full

RewriteRule ^Forums/(.*) http://example.com/forums/$1 [R]

That is to say, they are identical after I whap myself upside the head and replace "example.com" with the real domain name

TheKiller




msg:4352738
 3:46 pm on Aug 17, 2011 (gmt 0)

ARRGGHH -_-"

Well, Im looking for a code that dos the thing im asking for.
My code sucks. Can you think on something else that works when doing this?


We are moving in cyrcles and its a bit annoying. It would be great if you could help.

lucy24




msg:4352812
 6:46 pm on Aug 17, 2011 (gmt 0)

Just add the leading slash to your target. If that isn't enough, add the full path.

TheKiller




msg:4352823
 7:32 pm on Aug 17, 2011 (gmt 0)

Thanks Lucy !
In time i had asked my host also.
They gave the fallowing:
RewriteRule ^Forums/(.*)$ http://www.example.com/forums/$1 [R=301,L]

Should i use yours or theirs ?
RewriteRule ^Forums/(.*) http://www.example.com/forums/$1 [R]

g1smd




msg:4352824
 7:34 pm on Aug 17, 2011 (gmt 0)

If it a redirect, then always add the protocol and domain name to the target URL. I mentioned this in my second post above.

lucy24




msg:4352834
 7:48 pm on Aug 17, 2011 (gmt 0)

If you don't say R=301 it will default to 302 (temporary redirect). You want 301.

In this context the trailing $ doesn't matter because Regular Expressions are greedy by default and will gobble up as much as they can. The $ is only needed when you need to constrain the rewrite to some specific ending.

TheKiller




msg:4352987
 7:27 am on Aug 18, 2011 (gmt 0)

Allright, Thanks You Two :-)
I ended up with this inside my .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]
RewriteRule ^Forums/(.*) http://www.example.com/forums/$1 [R=301,L]

Can anyone of you see any errors in there?

g1smd




msg:4352989
 7:45 am on Aug 18, 2011 (gmt 0)

Two errors. Use the Live HTTP Headers extension for Firefox to see this in action.

If you request
example.com/Forums/somepage you will first be redirected to www.example.com/Forums/somepage and then to www.example.com/forums/somepage

This unwanted two step redirection chain is a big problem.

To fix it, always ensure that redirects are listed from most specific (affects the least number of URLs) to most general (affects the least number of URLs) and that the www/non-www redirect is the last redirect of all.



There's another more serious problem. Requests for
example.com will correctly redirect to www.example.com but other non-canonical requests will create an infinite loop.

In particular, a request for
example.com:80 will be redirected to www.example.com:80 and since this is not "exactly" www.example.com it will be redirected to www.www.example.com:80 and then to www.www.www.example.com:80 and so on.

This also occurs for other domains that resolve to your server, such as:
foobar.com => www.foobar.com => www.www.foobar.com => www.www.www.foobar.com looping forever.

In place of
%{HTTP_HOST} in the RewriteRule target URL, I would state www.example.com instead.
g1smd




msg:4353207
 7:23 pm on Aug 18, 2011 (gmt 0)

to most general (affects the least number of URLs)

should say
to most general (affects the most number of URLs)

TheKiller




msg:4353287
 10:08 pm on Aug 18, 2011 (gmt 0)

Hello g1smd, right now im too drunk to think well..........
Would you have an idea whats to fix on the script im using?

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