Looking for tips: Conditionals ReWrite in .htaccess?

Hi,

I am currently removing “index.php” and “folder” from the uri like so:


...
RewriteRule ^(.*)$ /index.php/folder/$1 [L]

The above works great.

Here is my predicament… I have other “folders” that I would like to access but the above RewriteRule gets in the way. For example:

http://site.com/folder2

The problem is that when I try to access “folder2”, the rewrite rule does this (under the hood):

http://site.com/index.php/folder/folder2

I guess I am wondering if there is a way to only “hide” the one “folder” if it is in the uri? Otherwise, if “folder” not in the uri, just remove “index.php”?

Does that make any sense?

Many many many thanks in advance! I greatly appreciate it!!! :slight_smile:

Cheers,
Micky

Oh, I should mention, these are not really folders… This is a CMS that I am using. The structure looks like this:

site.com/index.php/section/

I just wanted to clarify that there are not any physical folders to speak of… I just was trying to keep the examples in my original post simple. :slight_smile:

Thanks!!!

Micky

Micky,


...
RewriteRule ^(.*)$ /index.php/folder/$1 [L]

Unless the … includes a RewriteCond to prevent a loop, this cannot work (and shouldn’t have the leading / in the redirection as that will make Apache look to the root directory of the server before your DocumentRoot). Just add more RewriteCond directives to avoid capturing/redirecting your folder2 et al.

Of course, when you do that, you’ll need to specify another RewriteRule with an accompanying RewriteCond (or in the regex) to redirect folder2 (as it’s not a real folder, it would otherwise issue 404’s).

Regards,

DK

Hi DK! Thanks for the pro help/tips and quick reply, I greatly appreciate it. :slight_smile:

Do you happen to have an Amazon wishlist setup? I would love to buy you a book or something. This is the least I can do to pay you back for all the times you have helped me out. :slight_smile:

Thanks again with the tips.

I am at work now, so I won’t be able to test until later… As soon as I can I will post back my test results to share with you and others.

I am pretty happy to know that what I want to do is possible. I was almost wondering if I was wasting my time trying to find a working solution.

Also, just to fill you in on the other bits of my .htaccess, here is the … part:

###### Remove index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Exclude EE files:
RewriteCond $1 !^(foo1\\.php|foo2\\.html|foo3\\.php|foo4\\.php) [NC]
# Exclude 3rd party files:
RewriteCond $1 !^(favicon\\.ico|robots\\.txt) [NC]
# Exclude EE folders
RewriteCond $1 !^(bar1|bar2|bar3)/ [NC]
# Exclude 3rd party folders
RewriteCond $1 !^(baz1|baz2|baz3|baz4|baz5|baz6|baz7|baz8|baz9)/ [NC]
# Remove index.php
RewriteRule ^(.*)$ /index.php/folder/$1 [L]

Note: The above was lifted from this article.

I can’t wait to play around with this! Thanks for kicking me in the right direction! :slight_smile:

Have a great day!

Cheers,
Micky

I could not resist tinkering before I dive into my work… I quickly took out the leading “/” and everything appears to be working without error. Thanks for that tip! I will have to pass this info along to others using the same rewrite code.

Much appreciated.

I will post back later with the rest of my findings.

Cheers!
Micky

Even though that code works it could be made a little bit more efficient:


###### Remove index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Exclude EE files:
RewriteCond %{REQUEST_URI} !^(foo1\\.php|foo2\\.html|foo3\\.php|foo4\\.php) [NC]
# Exclude 3rd party files:
RewriteCond %{REQUEST_URI} !^(favicon\\.ico|robots\\.txt) [NC]
# Exclude EE folders
RewriteCond %{REQUEST_URI} !^(bar1|bar2|bar3)/ [NC]
# Exclude 3rd party folders
RewriteCond %{REQUEST_URI} !^(baz1|baz2|baz3|baz4|baz5|baz6|baz7|baz8|baz9)/ [NC]
# Remove index.php
RewriteRule .? /index.php/folder%{REQUEST_URI} [L]

The difference between this and the code you just posted is that your code has to actually perform a regular expression for (.) and store that value in memory, and use it in the RewriteConds, while %{REQUEST_URI} already has that value and doesn’t rely on that evil :redhot: (.) :redhot:

Wow! Awesome! :slight_smile:

Thanks so much ScallioXTX!!! I will modify my code later today and report back the results. :slight_smile:

I definitely plan on sharing all of these optimizations/modifications with others who use the same CMS, so I really appreciate all the help.

Have a great day!

Cheers,
Micky

OMG, it took me all night, but I think I found something that works:

###### Remove folder1:
RewriteCond %{REQUEST_URI} !^/(folder2) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php/folder1%{REQUEST_URI} [L]

###### Remove index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Exclude EE files:
RewriteCond $1 !^(foo1\\.php|foo2\\.html|foo3\\.php|foo4\\.php) [NC]
# Exclude 3rd party files:
RewriteCond $1 !^(favicon\\.ico|robots\\.txt) [NC]
# Exclude EE folders
RewriteCond $1 !^(bar1|bar2|bar3)/ [NC]
# Exclude 3rd party folders
RewriteCond $1 !^(baz1|baz2|baz3|baz4|baz5|baz6|baz7|baz8|baz9)/ [NC]
# Remove index.php
RewriteRule .? index.php%{REQUEST_URI} [L]

Here’s the lowdown:

First, I remove the “folder1” and ignore any requests to “folder2”.

Next, I remove “index.php”.

Before, I was removing “index.php” and “folder1” in the same RewriteRule… I thought it made more sense to break these apart… Reasoning: I always want to remove “index.php” no matter what the URL is, but I only want to remove “folder1” if “folder2” is not being requested.

Of course, my logic could be totally silly looking to ya’ll pros… If you see anything out of the ordinary, I would love some feedback.

Also, thanks to DK and ScallioXTX for the tips! Much appreciated!!! :slight_smile: :spf:

Cheers,
Micky

I forgot to mention… I think the hardest thing for me when it comes to writing .htaccess is that I don’t have the ability to add scafolding so I can see what variables/values and such I am working with… A lot of my time was spent throwing things against wall to see if it sticks! :smiley:

Any tips for “printing” values when working with .htaccess? It would be pretty cool if I could print/echo/alert things like the %{REQUEST_URI} in order to see what I am working with.

I did read somewhere that you could setup a log file, but unfortunately I don’t have access to the httpd.conf file on my host.

Thanks again!

Cheers,
Micky

I 302 to google with the string I want to see.


RewriteRule something http://www.google.com/?q=%{REQUEST_URI} [L=302]

That page doesn’t exist on google and will show the %{REQUEST_URI} in the URL just nicely.
Why google? No idea :smiley:

Oooh, great tip!!! Thanks ScallioXTX, much appreciated. :slight_smile:

You are truly the %{REQUEST_URI} maestro! :spf: :rofl:

On a more serious note, your tip on using %{REQUEST_URI} was pretty awesome. I love that I am no longer using the evil (.*)! :eye:

Have an excellent day!!!

Cheers,
Micky

Aw, using an R=301 flag will show your redirection - fantastic for testing but remove it for production as most webmasters like to hide redirections. In other words, no reason to piss-off Google like that!

Thanks, Micky (D K Lynn at Amazon). My PM box is full (of TL PM’s so it’ll take a while to clear but you can e-mail me if you like).

Regards,

DK