RewriteRule question


I am working on a server that has a CMS installed by the previous developer.

For security reasons, all of the CMS system files are kept on the server root, and then folders such as /images and /css are kept in /app/webroot/

The .htaccess looks like this

<IfModule mod_rewrite.c>
   RewriteEngine on	
    RewriteRule    (.*) app/webroot/$1 [L]	

I have created a blog in /app/webroot/blog, which I can access using

…but the URL in the address bar changes to

Is there anyway in .htaccess to keep the blog address as ?

Any tips to put me in the right direction would be appreciated.

Many thanks.

    RewriteCond &#37;{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Yup, thar she blows. That’s where your redirection is coming from. Wordpress has this kind of thing. It doesn’t loop because only stuff that isn’t a directory or a file is going to index.php who is looking at the (id or whatever) to know where to fetch the file.

Which is about all I know about PHP script redirecting stuff, lawlz.

Is it possible to create a rule just for the /app/webroot/blog directory which would rewrite is to just /blog?

Yes, I think so, but I may not be good enough to give you something that won’t interfere or get interfered with your script.
Because you have the two rewrite conds checking if it’s not a file or a dir, I’m no sure if yet another rewritecond at the end can deal with “blog”. But since “blog” seems to be getting passed to the index.php script, you might be able to do something like

RewriteRule ^blog webroot/app/blog [L]  (or ^(blog) webroot/app/$1 [L] I guess)
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

That doesn’t feel exactly right to me, and actually for something like “blog” which is literal and doesn’t need regex matching would normally just be aliased or something, except that I’m thinking it needs to be a rewrite rule right before the index.php line (because then “blog” will get snagged and rewritten before the rewrite engine gets to the next line, and should be stopped by the [L] flag).

Otherwise the other thought I had was another rewritecond checking specifically for the word “blog” followed by the [S] (skip) flag. So that the index.php line is skipped, and then afterwards a rewriterule sending blog to webroot/app/blog.

I’m a noob in this Apache stuff, but much more noobish when it comes to automatically-generated rewrites as come with stuff like WP. But I guess the worst that could happen while trying these out is that you loop and make your server take a puke. Restart and change the rules then.

If you get something working, post it here anyway… our resident guru DK Lynn can check it for gotcha’s or obvious stupid stuff I’ve steered you towards.

Then also your other two (earlier) rewrite rules can be removed, including the ifmodule wrappers (well, remove those anyway).

Hi Stomme poes,

Thanks for that. I learned a few things from you with this thread anyway!

After messing around with Apache and that index.php file for a while, I decided that it would be best to create a new sub domain, and re-upload the Wordpress blog there.

In hindsight, I should have done this in the first place. It´s best to keep Wordpress and the CMS completely separate.

Thank you so much for your help.

Dank u wel :slight_smile:

I’ll point out something first:

[b]<IfModule mod_rewrite.c>[/b]
   RewriteEngine on	
    RewriteRule    (.*) app/webroot/$1 [L]	

If that code actually lets you get to when you type in, then lose the <IfModule> tags. It forces your server to check, every single time if you have mod_rewrite. You obviously do, so don’t make the server check anymore.

Second, if you are seeing the real url ( then you are not getting rewritten. You are getting redirected.

Possibly there is another rule further down with the [R] flag? Or possibly a script in your CMS is manually redirecting and the rewrite rule is getting ignored entirely?

The rule posted above isn’t great, but there’s no obvious reason why it should be redirecting rather than rewriting.

RewriteRule (.) app/webroot/$1 [L]
That (.
) actually also matches “app/webroot/blog” (and it also matches absolutely nothing as well) so the only thing saving you from infinite looping is that [L] flag.

Is everything on the site sitting in app/webroot? Or only directories like blog/, css/, and images/ ? Are there any other rewrite lines? What is your site’s situation? What is the CMS?

Hi Stomme poes,

Thanks for your detailed response. You´re right, I found two more .htaccess files further down the tree.

In /app

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]

in /app/webroot

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ [COLOR="Red"]index.php[/COLOR]?url=$1 [QSA,L]

As for the CMS, it is a custom built and used the CakePHP framework, hence all these rewrites.

The index.php file in the root directory is the CakePHP Requests collector

/* SVN FILE: $Id: index.php 7805 2008-10-30 17:30:26Z AD7six $ */
 * Requests collector.
 *  This file collects requests if:
 *	- no mod_rewrite is avilable or .htaccess files are not supported
 *	-/public is not set as a web root.
 * PHP versions 4 and 5
 * CakePHP(tm) : Rapid Development Framework (
 * Copyright 2005-2008, Cake Software Foundation, Inc. (
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * @filesource
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (
 * @link CakePHP(tm) Project
 * @package       cake
 * @since         CakePHP(tm) v 0.2.9
 * @version       $Revision: 7805 $
 * @modifiedby    $LastChangedBy: AD7six $
 * @lastmodified  $Date: 2008-10-30 18:30:26 +0100 (czw, 30 pa&#378; 2008) $
 * @license The MIT License
 *  Get Cake's root directory
	define('APP_DIR', 'app');
	define('ROOT', dirname(__FILE__));
	define('WEBROOT_DIR', 'webroot');
 * This only needs to be changed if the cake installed libs are located
 * outside of the distributed directory structure.
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
	if (function_exists('ini_set')) {
		ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS);
		define('APP_PATH', null);
		define('CORE_PATH', null);
	} else {
		define('APP_PATH', ROOT . DS . APP_DIR . DS);
	require CORE_PATH . 'cake' . DS . 'basics.php';
	$TIME_START = getMicrotime();
	require CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php';
	require LIBS . 'object.php';
	require LIBS . 'inflector.php';
	require LIBS . 'configure.php';

	$bootstrap = true;
	$url = null;
	require APP_DIR . DS . WEBROOT_DIR . DS . 'index.php';

The the files in /app/webroot can be accessed through the browser. That´s why I uploaded the Wordpress blog there - I didn´t want to interfere with the current CMS.

All files in /app and /webroot seem to be related to the CMS and CakePHP.

Not sure where to go from here. Is it possible to create a rule just for the /app/webroot/blog directory which would rewrite is to just /blog?

Thanks for your help on this.