@gaillimh posted a new topic, subject "RewriteRule question". It read:

   Hello,
    
    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
    [code] 
        <IfModule mod_rewrite.c>
           RewriteEngine on	
            RewriteRule    (.*) app/webroot/$1 [L]	
        </IfModule>
        
    [/code]
    
    I have created a blog in /app/webroot/blog, which I can access using
    
    domain.com/blog
    
    …but the URL in the address bar changes to
    
    domain.com/app/webroot/blog
    
    Is there anyway in .htaccess to keep the blog address as domain.com/blog ?
    
    Any tips to put me in the right direction would be appreciated.
    
    Many thanks.

So @Stomme_poes replied

    [code]
            RewriteCond &#37;{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
        
        
    [/code]
    
    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
    [code] 
        RewriteRule ^blog webroot/app/blog [L]  (or ^(blog) webroot/app/$1 [L] I guess)
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
        
    [/code]
    
    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).

@gaillimh replied

    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 blog.domain.com, 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

@Stomme_poes replied

    I'll point out something first:
    [code] 
        [b]<IfModule mod_rewrite.c>[/b]
           RewriteEngine on	
            RewriteRule    (.*) app/webroot/$1 [L]	
        [b]</IfModule>[/b]
        
    [/code]
    
    If that code actually lets you get to yourdomain.com/app/webroot/blog when you type in yourdomain.com/blog, 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 (domain.com/app/webroot/blog) 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?

After which, @gaillimh added

    Hi Stomme poes,
    
    Thanks for your detailed response. You´re right, I found two more .htaccess files further down the tree.
    
    In /app
    [code] 
        <IfModule mod_rewrite.c>
            RewriteEngine on
            RewriteRule    ^$    webroot/    [L]
            RewriteRule    (.*) webroot/$1    [L]
         </IfModule>
        
    [/code]
    
    in /app/webroot
    [code] 
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ [COLOR="Red"]index.php[/COLOR]?url=$1 [QSA,L]
        </IfModule>
        
    [/code]
    
    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
    [code] 
        <?php
        /* 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 (http://www.cakephp.org)
         * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
         *
         * Licensed under The MIT License
         * Redistributions of files must retain the above copyright notice.
         *
         * @filesource
         * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
         * @link          http://www.cakefoundation.org/projects/info/cakephp 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       http://www.opensource.org/licenses/mit-license.php The MIT License
         */
        /**
         *  Get Cake's root directory
         */
        	define('APP_DIR', 'app');
        	define('DS', DIRECTORY_SEPARATOR);
        	define('ROOT', dirname(__FILE__));
        	define('WEBROOT_DIR', 'webroot');
        	define('WWW_ROOT', ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS);
        /**
         * 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')) {
        		//define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
        		define('CAKE_CORE_INCLUDE_PATH', ROOT);
        	}
        	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);
        		define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . 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';
        ?>
        
    [/code]
    
    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.


{'source': 'discourse', 'url': 'https://www.sitepoint.com/community/t/rewriterule-question/63251', 'title': 'RewriteRule question', 'views': 2381, 'has_accepted_answer': False, 'word_count': 1315, 'like_count': 0, 'reply_count': 2, 'participant_count': 2, 'posts': 5, 'tags': '', 'author': '@gaillimh', 'participants': '@gaillimh, @Stomme_poes', 'image_url': [None], 'created_at': '2010-07-27T12:09:09.000Z', 'last_posted_at': '2010-07-29T17:54:38.000Z'}