Forum Moderators: phranque

Message Too Old, No Replies

What should be a simple redirect question

need some help with an .htaccess code

         

calldaffer

10:40 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



Forgive me guys, I have spent the last 30 minutes sifting through the pages in here looking for an answer but I couldn't find it. Here is what I want. I want to redirect all my /main joomla pages to my .com The .htaccess code that I placed on my .com will redirect the /main page over to the dot com but if you click anything on the page you get a 404 error. When you look at the URL that its going to the links are leaving off the /main which is making it look like all my pages are down but the main page. What did I do wrong with the code?

Here is what I have in my .htaccess file:

RewriteEngine On

#Internally rewrite / to /Main_Page
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^.*$ /main/$1 [L]

UserFriendly

11:34 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



Hello, calldaffer.

If you want to permanently redirect your visitors to a new URL, then you need to send a 301 redirect status code, which you indicate with an R flag in the RewriteRule options. Like this:

RewriteRule ^.*$ /main/$1 [L,R=301]

Without the R flag, Apache silently rewrites its filepath to find the target, and the browser is left in the dark. Which usually means that all the links on the page are broken unless you write rewrites that silently redirect all of those too.

UserFriendly

11:37 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



Actually, looking at your RewriteRule, it seems that you are catching visitors to the domain in which the .htaccess file is located and sending them to the /main/ folder in that same domain. Which doesn't match with your description.

I think I've misunderstood your goal. Could you explain further?

calldaffer

11:47 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



Ok, lemme try again (also I can put the file back in and post the url so everyone can see what its doing if that is allowed)

What I wnat is when you go to www.example.com I want a .htaccess file to pull www.example.com/main and display it on www.example.com which is what the above code does.

But the problem I run into is when you are at www.example.com and you click on any link I get a page cannot be found because its trying to go to www.example.com/contact.html when it should really be going to www.example.com/main/contact.html

the .htaccess code that I am using isn't send people to the right urls for some reason. Does that explain it? Like I said, I'd post the real url but I haven't looked at this sites rules so I dont know if thats acceptable or not.

g1smd

11:50 pm on Jul 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to be clear as to what you mean, do you need an Internal Rewrite or do you need an External Redirect?

A rewrite means that when a user asks for URL A, that the content comes from B, but the user still sees the URL as being A.

A redirect means that the user asks for A, and the server says it is over there at B. The browser then re-requests that content from B, the new URL.

calldaffer

11:57 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



I want a rewrite (thanks for the explanation between the two, I am a newb at this)

calldaffer

1:48 pm on Jul 8, 2007 (gmt 0)

10+ Year Member



sorry to bump but I gotta get this resolved soon. Right now I'm back to a doorway and I really want to avoid that if possible.

jdMorgan

6:45 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps this is more like what you're looking for:

RewriteEngine on
#
# Internally rewrite requests for all pages in "/" to pages in /main/
#
# If not already rewritten to /main/
RewriteCond $1 !^main/
# Prepend "/main" to the requested URL-path
RewriteRule (.*) /main/$1 [L]

Now this code will take *any* request to the server, and add /main" to the requested URL-path. It will do so for all requests for pages, images, external CSS files, external JavaScript files, media files, etc. So be sure that that is what you want: If there exceptions where you do not want to prepend "/main" to the path, you will have to identify all of them.

Jim

calldaffer

7:14 pm on Jul 8, 2007 (gmt 0)

10+ Year Member



Oh man, that is so close. Everything in that works except its blocking access to my forums.

Is there anyway to add something to the above code that would only do that to whats in my /main folder?

If not then what I need it to do is exclude my /members folder. That is where my forum resides and when I add your code above it tells me there is nothing there when I try to get to my forums.

g1smd

7:19 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to add something like this in there:

RewriteCond $1 !^forums/

where forums/ is the name of the folder where the forums actually reside.

The rule will then work for anything that isn't already requesting either "main" or "forums".

jdMorgan

7:25 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh that's not *my* code -- It's *your* code... Adjust to suit. :)

As I said, you must precisely define what URL-paths you do and do not wish to rewrite, and then implement that. Writing the code is easy, it's defining the problem correctly and thoroughly that is hard.

As g1smd points out, for any subdirectory *not* to be rewritten, you'll need to add an exclusion in the form already given for /main/ above. If the list of exclusions gets too long, then you probably need to back up and try a different approach.

Jim

g1smd

7:34 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, yes. Defining the problem.

THAT is the really difficult part - many is the time that something I coded wasn't "working", because what I actually coded it to do, wasn't exactly what I needed it to be doing.

To be clear, the code works exactly as coded, not what you thought or hoped it might do. Careful testing is in order after implementation to verify that everything works exactly as it should for all formats of URL that you throw at it. As well as testing for things you expect it to do, you also need to throw a few whacky URLs at it to see what it does with those. Many is the time that I have missed something.

[edited by: g1smd at 7:53 pm (utc) on July 8, 2007]

calldaffer

7:39 pm on Jul 8, 2007 (gmt 0)

10+ Year Member



you rock JD, its in and seems to be working as it should. I will tinker around with it and if I run into any more snags I'll be back to this tread. Thanks a million, just out of curiosity...were there similair threads already here that addressed this problem. Just curious if I overlooked them.

jdMorgan

8:41 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many threads that deal with rewriting subdomains to subdirectories. This involves the same principle of prepending path info to the requested URL-path, and also brings up the problem of preventing the code from prepending the same additional path info recursively, which it will do unless you explicitly prevent it.

There are other threads, I'm sure, on "moving content into [one or more] subdirectories." The problem is that there is no standard "name" for this process/project/goal. I could declare that it should be called "Prepending fixed path info to requested URL-paths" based on using the right terms in the right ways, but I doubt that anyone would ever post a thread title like that. :)

A few of these threads also point out a potential problem --usually minor-- that may or may not need to be addressed. In most cases, the cure is worse than the problem, because it adds complexity.

The problem is that it will not be possible to add a subdirectory in /main that is also called /main. If you did attempt to do this, then if the original non-rewritten request was for example.com/main/foo.html, and you *did want* that to be rewritten --like the URLs we've already discussed-- to example.com/main/main/foo.html, there would be a problem; Because of the exclusion of requests starting with "/main/" from being rewritten, the rule would never be invoked, and you'd end up serving (or trying to serve) the file at /main/foo.html instead of the one you really wanted, located at /main/main/foo.html

This was termed "obscuration" by another poster, and there are several fixes. But they are apparently somewhat server-configuration dependent, and also overly-complicated. Therefore, I suggest their use only when unavoidable.

So, back to your question, yes there are threads that discuss *most* of what you wanted to do, but in terms of an unrelated goal. That happens a lot -- discussing bits and pieces of solutions to this and that problem. It's due to the compactness, flexibility, and power of the combination of mod_rewrite and regular expressions.

Jim

g1smd

9:00 pm on Jul 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is another approach ... to make the domain name resolve directly to the /main/ folder on the server, and then add the /forums/ folder as an alias.

That's a completely different way of doing things; and setting up things that way might not be available in all types of hosting plan. You might need more access to the server configuration.

There are always many different ways to achieve the same result, some easier than others, and each with their own special advantages and disavantages.