Forum Moderators: phranque
Here is what I have in my .htaccess file:
RewriteEngine On
#Internally rewrite / to /Main_Page
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^.*$ /main/$1 [L]
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.
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.
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.
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]
Jim
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.
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
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]
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
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.