Avatar of Kosta
 asked on

Simple rewrite rule


Please help me to configure a simple mod-rewrite rewrite rule.
I have a folder, named "pyramide" in my httpdocs folder, where my site files are. So the site is reachable by this url: http://example.com/pyramide/ .
I can not move my site physically one level up because of some technical reasons. But I need it to be reachable by the domain root: http://example.com/

Thank you.
Apache Web ServerPHP

Avatar of undefined
Last Comment
Steve Bink

8/22/2022 - Mon
Bernard Savonet

I assume that you cannot move the site one level up, but that you have access to the root directory.

When someone reaches your site at http://example.com/ you want that this is translated as http://example.com/pyramide/

You can do that in 2 ways:
1 - create an Apache mod-rewrite within and .htaccess file
2 - create a php rewrite within index.php (I assume you have php since you post in tyhe php area)

The basic expression for 1 would be
Redirect 301 ^http://example.com$ http://example.com/pyramide/

The basic expression for 2 would be a file index.php containing just
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://example.com/pyramide/");


Hello Fibo.

Thank you very much for the reply, but I think I did not explain my question clear enough. I don't want to redirect visitors, I want to rewrite the url, so for example if the visitor types this address: http://example.com/foo/bar/", I want him to continue to see this address in his browser's address bar, but I want him to see a content of http://example.com/pyramide/foo/bar/. I was told this is possible by using the Apache's mod-rewrite directive in a .htaccess file. But I can not figure out how exactly to set it up.

Thank you.
Bernard Savonet

Hmmm... it is not really possible.

What would be possible (but don't do it): create a frame so that the visitors don't see that they are at some othe page. Don't do it, because it is really a problem for search engines an navigation

If you are on a page, this is the address that would be displayed by the browser... otherwise phishing would be lots easier!
You can ask Apache to redirect to some other page, but which one will be visible.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

But many sites now use so called Human Readable URLs technique, and it is usually based on a mod-rewrite Apache module. What it does is rewriting addresses like http://example.com/index.php?catalog=foo&article=bar to be seen in a browser as http://example.com/foo/bar/ or http://example.com/catalog/foo/article/bar/
I thought I need something similar, even much simpler...
Steve Bink

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Bink

Note that the rule must be in your server's document root .htaccess file, or in the server config.  This will not work if it is placed in the sub-directory's .htaccess file.