• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1493
  • Last Modified:

ReWrite Rule for everything except specific directories

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.
  • 2
  • 2
1 Solution
What are you looking for http://www.yoursite.com/CA 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.
MaybeItsJeremyAuthor Commented:
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 yoursite.com/CA needs to actually go to yoursite.com/CA instead of yoursite.com/index.php.
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.
MaybeItsJeremyAuthor Commented:
You got me on the right path! Thank you.

The end result that works for me is:

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now