RewriteCond Help - Condition mis-evaluating-Not as expected

New to mod_rewrite? This is a good place to start.

RewriteCond Help - Condition mis-evaluating-Not as expected

Postby .arik. » Fri Mar 28, 2008 11:11 am

Hi to everyone.

This is my first approch to the mod_rewrite, and I am felling lucky for finding a forum dedicated to this theme. Nothing less as expected for this mod.

My problem here is that the RewriteCond is not being evaluated as I expected. I need to redirect all traffic from dom1 to dom2, except those trafic in dom1/ss/ which has to be processed as normal.

This is the code:

Code: Select all
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond  REQUEST_URI !^/ss(/.*)
RewriteRule ^(.*) http://dom2.com/$1 [R]


But it does redirect all traffic to dom2, even when http://dom1.com/XXX.xxx is requested

Does anybody know the reason?

I'm actually new to mod_rewrite, and I couldn't find the answer on the net.

Thanks a lot in advance.
.arik.
 
Posts: 4
Joined: Fri Mar 28, 2008 7:41 am

Postby richardk » Fri Mar 28, 2008 12:27 pm

Variables go in %{}, eg. %{REQUEST_URI}.

But you can do it in one line
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule !^ss(/.*)?$ http://example.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby .arik. » Fri Mar 28, 2008 3:17 pm

Thanks richardk

It works perfectly.

Now, I've introduced myself into another problem.

The second part of the story is that all traffic on the /ss/ virtual folder has to gather the filenames on /. I mean, a silently redirect (no 3xx redirect) from /ss/ virtual folder to / phisical folder.

I think I am near, but... Oh! my god.... How painful is the rewrite mod!!!!

Ok. Here's what i have

Code: Select all
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond  REQUEST_URI !^/ss(/.*)
RewriteRule ^(.*) http://dom2.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/ss(.*)
RewriteRule ^ss/(.*) $1 [NC,L]


The problem here is that i mis-understood the use of the [L] tag. Now I realize that does not break a "thread" of rewritings, but just stop a single iteration.

Then the question is: is there a way to accomplish my goal?

Here is an example of what i need, if I didn't explained it well.

Code: Select all
1.- Access http://dom1.com/index.html should redirect via 301 to http://dom2.com/index.html [ACCOMPLISHED THANKS TO richardk]

2.- Access http://dom1.com/ss/index.html should show (just silent rewrite, no 3xx redirection) the phisical content of http://dom1.com/index.html, but not redirect to dom2.com


I think it is a beautiful problem. I wonder if it is possible to accomplish.

Thanks a lot for your help.
.arik.
 
Posts: 4
Joined: Fri Mar 28, 2008 7:41 am

Postby richardk » Sun Mar 30, 2008 10:25 am

The L flag stops all further RewriteRules from being processed. But if a rewrite occurs in a .htaccess file (or any redirect occurs) a whole new request has to be made (internally by Apache for rewrites) to get the page the user now needs. This is because .htaccess files are processed very late, if you use mod_rewrite in the main server configuration or a <VirtualHost> this is not the case.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore internal rewrites, like for /ss/* to /*.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^ss(/.*)?$ http://example.com/$1 [NC,R=301,L]

RewriteRule ^ss(/(.*))?$ /$2 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby .arik. » Sun Mar 30, 2008 12:37 pm

Thanks again richardk.

I modified this a bit, because of broken images, css issues, and not rewriting properly. I think this mis-rewrite was caused by the RewriteRule !^ss(/.*)?$ ... line. It rewrote something like http://dom1.com/file.php?op=1 to http://dom2.com/?op=1

This is my final rewrite code that worked. Do you see any problem? Maybe something to optimize??

Code: Select all
# Ignore internal rewrites, like for /ss/* to /*.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond  %{REQUEST_URI} !^/ss(.*)
RewriteRule ^(.*) http://dom2.com/$1 [NC,R=301,L]

RewriteRule ^ss/(.*) $1 [nc,l,qsa]



Thanks a lot!!!!!!!!!!!!!!!!!!!! :D
.arik.
 
Posts: 4
Joined: Fri Mar 28, 2008 7:41 am

Postby richardk » Sun Mar 30, 2008 1:21 pm

Fixed
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^ss(/.*)?$ http://example.com%{REQUEST_URI} [NC,R=301,L]

RewriteRule ^ss(/(.*))?$ /$2 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 4 guests

cron