htaccess code file for pretty URLs

Hi, to enable "pretty URLs", C5 recommends the following code in the .htaccess file:

# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /installation_directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
# -- concrete5 urls end --

Yii, a PHP framework that I use, to achieve the "pretty URL" effect of removing index.php from the URL, recommends the following code:

# -- yii urls start --
<IfModule mod_rewrite.c>
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
# -- yii urls end --


I know they are 2 different systems but you don't have to be a genius to realize that both portions of code are doing the same thing.

I want to bring this out because the C5 code recommendation includes the relative path where the C5 site is installed e.g. the "RewriteBase /installation_directory/" part. This means that you have to check/change this code whenever you deploy your C5 site from local to live. I'm guessing that changing this is a common scenario.

So, my question is... is including the subdirectory in the code really necessary? I commented that line months ago and my C5 site is still working fine with no problems that I can see.

If that's not really necessary and the C5 team removes that line, it would be one less thing to take into account when moving a C5 site (from local to live, migrating to a new server, etc)

View Replies:
mhawke replied on at Permalink Reply
I'm not sure about your set-up but I have a site that's installed in a directory off the root so my core files are here:

I just tested it and if I remove the line you're talking about from my .htaccess file, my pages all 404.

I also add "define('DIR_REL', '/C5');" to my site.php file
kesnav replied on at Permalink Reply
Thanks for reporting your case.

I tested removing the .htaccess line in a fresh C5 and on an old v5.5.x site and it seems to work fine.

The 404 error you get must be related to the DIR_REL option you set. Is that option really needed? I can put my C5 site in a subdirectory in my localhost environment and I don't need to tell C5 in which subdirectory it is... I have yet to test that on a subdirectory in a live server though.
mhawke replied on at Permalink Reply
I think the DIR_REL is used by a number of blocks and they fail if that isn't defined. I can't remember which ones off the top of my head.

I just tested my site by removing the define('DIR_REL... and removing the line from the .htaccess and my site fails with 404's.

Maybe it depends on the server. I haven't thought about it cuz if it ain't broke, I don't fix it.

If the C5 core team recommends I do something, I generally do it because they've seen a lot more edge cases than I ever will.
mhawke replied on at Permalink Reply
Just a thought but are you setting up your localhost to land on the subdirectory?
kesnav replied on at Permalink Reply
Hmm, I haven't set up my localhost to do anything special. I install C5 in, say:


And I can move it to a different subdirectory without any modifications (this is, without the "RewriteBase" line) and it works fine:


I just find strange having to tell C5 in which sub directory it is. I don't understand it, since it can just use relative paths to its current directory, wherever it is, no matter how deep it is relative to the "root".

I will try later this on a live server.

Edit: I also don't think that DIR_REL is required since it doesn't come by default in the site.php file... at least not in my C5 install.
mhawke replied on at Permalink Reply
Two things...

1) Whenever I set up my localhost for a new website, I need to tell it where the files are so my server is already landing right on the index.php file. The nameservers for a real site just land users on the true root of the site.

2) DIR_REL is not included in the default set up because a normal installation is in the root, not in a sub-directory. If you type DIR_REL into the search box you will find plenty of explanations.
kesnav replied on at Permalink Reply
Hi, two things:

1) I made the tests and the C5 site works fine on different subdirectories both on my localhost and live server environment, without having to specify the subdirectory anywhere.

2) I found a confirmation from the C5 core team that the BASE_URL and DIR_REL are not required anymore since C5 tries to guess them:

I think this RewriteBase /installation_directory/ might not be necessary for most sites. I would like it if somebody can confirm this. I know it works for me but I'd like to be sure that this is intented behavior and not just that I'm lucky this time. I'd like not to specify the installation directory so I can deploy from local to live without having to change that file.
mhawke replied on at Permalink Reply
All I can confirm is that I have a site that's in a sub-directory off the root. It works fine if I leave out the DIR_REL definition in site.php but it fails if I comment out the "RewriteBase /installation Directory/". When I say 'fail', I mean I can always land on my homepage but as soon as I navigate off the homepage I get 404's.