ReWrite Rule for everything except specific directories

Posted on 2006-05-22
Last Modified: 2012-08-13
Here is my current .htaccess file:

Options +Indexes
Options +FollowSymlinks
RewriteEngine on

RewriteRule ^favicon.ico - [L]
RewriteRule ^images - [L]
RewriteRule ^design/images - [L]
RewriteRule ^design/style - [L]

RewriteRule ^(.*)$ index.php [QSA,L]

Every address is rewritten to my index.php file except the specific direcotires listed... Now I need to add something to it. I am going to have a directory for each state in the USA. (CA, NC, FL... etc...). I need to have a rewrite rule that only matches letters, and is limited to 2 characters. I have tried one example to no avail:

RewriteRule ^([a-zA-Z]{2})/? - [L]

Of course, that has to come before my index.php rewrite rule...And keep in mind, the ending slash is optional, someone may directly type in the address and omit the end slash, I need to cater to that.

Any help is appreciated.
Question by:MaybeItsJeremy
    LVL 9

    Expert Comment

    What are you looking for to redirect to?

    RewriteRule ^[a-zA-Z]{2})$ /$1/ [R]
    RewriteRule ^([a-zA-Z]{2})/? xxxxxxxxxxxx [L]

    The above might be what you need if you can post what you are redirecting to, unless I missed it above.

    Author Comment

    I don't want it to redirect to anything, I just want it to have a last rule, so it will not be directed to the index.php file. Basically needs to actually go to instead of
    LVL 9

    Accepted Solution

    I'm not a rewrite rule wiz, but I think what you want is a rule like:

    RewriteRule ^[a-zA-Z]{2})$ /$1/ [R]
    RewriteRule !([a-zA-Z]{2})/? index.php [QSA,L]

    The first rule will turn a 2 char (state) into a 2 char state with a trailing slash.
    The 2nd rule will redirect to index.php if the URL is NOT a 2 char state with a trailing slash.

    Like I said, I'm not a wiz but figured I'd offer this up and maybe it's right or close enough to get you going in the right direction.

    Author Comment

    You got me on the right path! Thank you.

    The end result that works for me is:

    RewriteRule ^([a-zA-Z]{2})/?(.*)$ - [L]

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    HTTPS Redirect Problem 17 57
    how to configure and install nginx ? 1 348
    Apache 2.2 on Windows 2008 32 43
    Increase size of permitted URI 4 36
    If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
    Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now