Forum Moderators: phranque

Message Too Old, No Replies

Using IF statement with Mod Rewrite

Mod rewrite with the IF statement

         

tommytx

2:21 am on Nov 14, 2006 (gmt 0)

10+ Year Member



Is there any way to do the IF statement below... if the word ebay or archives is in the url I want to do the top part and if not the bottom part....

AddType application/x-httpd-php .htm .html

if word "ebay" or word "archives" in URI then Do this
*****************************************************
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ebay /help/support.pl
RewriteRule archives /fina/home.pl

### This will change all spaces in URI to dashes
RewriteCond %{REQUEST_URI} ^(.*)\ (.*)$
RewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
RewriteCond %{ENV:space_replacer}!^$
RewriteCond %{ENV:space_replacer}!^.*\ .*$
RewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]

Else Do this
**********************************
## This junk is required to run Blog Solutions
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fina(.*)$ - [L]
RewriteRule ^help(.*)$ - [L]
RewriteRule ^manage(.*)$ - [L]
RewriteRule ^batch(.*)$ - [L]
RewriteRule ^export(.*)$ - [L]
RewriteRule ^templates(.*)$ - [L]
RewriteRule ^favicon\.ico(.*)$ - [L]
RewriteRule ^robots\.txt$ - [L]
RewriteRule ^plain\.php$ - [L]
RewriteRule ^(.*)$ index.php

Thanks for any help....

The space replacer above works great but when the BS stuff is inserted the space replacer won't work... I think if I could operate them depending on if statement all would work... but not sure how to use the if statement in htaccess..

jdMorgan

3:06 am on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no general "IF" qualifier in .htaccess. Just use an additional RewriteCond in the first ruleset, and then add a rule qualified by the negative of that same RewriteCond ahead of the last rule, just as you've done for the other exceptions.

That space replacer code looks very complicated for what it needs to do, BTW, and it also looks like it will only fix a single space.

Jim

tommytx

3:29 am on Nov 14, 2006 (gmt 0)

10+ Year Member



Thanks jd for the help... that is why I always come here as I know you are the world famous htaccess expert...

The replacer works great on an unlimited number of words...for example /ebay/my-real-estate-network-team-in-Florida.htm

Try it you might like to put it in your tool box of tricks..

I just have no idea how to structure what you said... let me try here and maybe you can get me on the right track....

eample of a URI that needs to be processed...

on the web page <a href="ebay/my car for sale.htm">my car for sale</a>
/ebay/my car for sale.htm (on the web page and clicked.)
/ebay/my-car-for-sale.htm (what you see in the browser line.)

Can I do something like this...

RewriteCond %{REQUEST_URI} ^ebay [or]
RewriteCond %{REQUEST_URI} ^archives
#now add all the top lines here

RewriteCond %{REQUEST_URI} ^!ebay [or]
RewriteCond %{REQUEST_URI} ^!archives
#now add all the bottom lines here

As you can see I have not idea what I am doing here... just groping for straws with the hope someone can see what I am attempting to do and bail me out...

jdMorgan

4:41 am on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I still don't see how that works -- the (now) third RewriteCond seems to prevent the rule from redirecting if the space_replacer variable contains remaining spaces, and no recursion mechanism (e.g. [N] flag) is apparent in that code. You might want to check to make sure that it still does handle multiple spaces, and that it only does one redirect no matter how many spaces are present, rather than one redirect per space... I'll experiment with that if I get some time, but it looks nothing like code I've previously written for that purpose.

Nevertheless, to answer the main question:

Add a RewriteCond to allow the first ruleset to take action only for 'ebay' or 'archives.' Then add a negative condition to prevent the blog stuff from taking action for 'ebay' or 'archives':


## This will change all spaces in URI to dashes
[b]RewriteCond %{REQUEST_URI} ebay¦archives[/b]
RewriteCond %{REQUEST_URI} ^(.*)\ (.*)$
RewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
RewriteCond %{ENV:space_replacer}!^$
RewriteCond %{ENV:space_replacer}!^.*\ .*$
RewriteRule ^.*$ http://www.example.com%{ENV:space_replacer} [R=301,L]
#
## This junk is required to run Blog Solutions
[b]RewriteCond %{REQUEST_URI} [i][/i]!(ebay¦archives)[/b]
RewriteCond %{REQUEST_URI} !^/(fina¦help¦manage¦batch¦export¦templates¦favicon\.ico$¦robots\.txt$¦plain\.php$)
RewriteRule ^(.*)$ index.php

I took the liberty of correcting the redirect syntax in the space replacer, and of compressing the exceptions for your blog rule into a single RewriteCond...

Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim