Jump to content

Order of mod_rewrite


danbopes

Recommended Posts

OK, I have a situation, where I want to use mod_rewrite in three places to finally end up at the url I want.

 

Sample URL:

demo1.example.com/home/index

 

In my Document Root I have:

RewriteEngine on

RewriteCond   %{HTTP_HOST}                                ^(?:www\.)?[^./]+\.[^./]+\.[^./]+$
RewriteCond   "%{HTTP_HOST}%{REQUEST_URI}"                !^(?:www\.)?([^./]+)\.[^./]+\.[^./]+/?\1/
RewriteRule   ^(.*)                                       %{HTTP_HOST}/$1  [C,DPI]
RewriteRule   ^(?:www\.)?([^./]+)\.[^./]+\.[^./]+/(.*)$   /$1/$2

 

In my demo1 folder I have:

RewriteEngine on
RewriteRule ^(.*)$ /dev/$1

 

In my dev folder I have (Code Igniter's rewrite):

RewriteEngine on
RewriteCond $1 !^(index\.php|images|user_guide|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

 

So the end result is that it demo1.example.com/home/index should goto /dev/index.php/home/index.

 

This semi works, but here is where the problem comes in. If the folder "home" exists, and there is a .htaccess file in there, it will process it before the parent (Which says go into the demo1 folder), and it ends up giving a 404. Is there a way I can fix this? I've tried adding the initial Document Root into the httpd.conf file, as well as in the <Directory></Directory> tags, and I ended up getting no rewrite, and same issue respectively when I did that.

 

How can I fix this?

Link to comment
Share on other sites

  • 2 weeks later...

It seems like you need a few [L]s in there. Know that it does not stop URL rewriting entirely - just the current pass. The rewritten URL goes back through the request handling process, and that may trigger mod_rewrite again. Without an [L] flag, the rewritten URL is tracked and other rules that were going to be processed still will be; if you're changing the paths as much as you are that may cause some rules to rewrite incorrectly.

 

/.htaccess, handles subdomains

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(?:www\.)?[^./]+\.[^./]+\.[^./]+$
RewriteCond %{HTTP_HOST}%{REQUEST_URI} !^(?:www\.)?([^./]+)\.[^./]+\.[^./]+/\1/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^ %1%{REQUEST_URI} [DPI,L]

(You can use %N to refer to something caught in a RewriteCond)

 

/demo1/.htaccess, rewrites everything to /dev

RewriteEngine on

RewriteRule .* /dev/$0 [L]

 

If that still doesn't work, add some [R]s and see where mod_rewrite is trying to go.

Link to comment
Share on other sites

I need it to chain, the problem is the order of how it rewrites stuff.

 

For example: demo1.example.com/home/index

 

First rewrite (In root) should say, hey, you need to goto /demo1/home/index

Second rewrite (In /demo1) should say, hey, you need to goto /dev/home/index

Third rewrite(In /dev) should say, hey, you need to goto /dev/index.php?/home/index

 

Problem is this, if the folder home exists, with a .htaccess file in it, the order then becomes:

First rewrite (In /home): /home/index.php?/home/index

Second rewrite (In root): /demo1/home/index.php?/home/index

Third rewrite (In /demo1): /dev/home/index.php?/home/index

 

It should go like the first one, and rewrite it to the demo1 folder, before trying to look in the home folder to begin with. That's the only rule I need to have go before the others, and putting it in the server config did not do anything. Please help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.