• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1487
  • 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.
0
MaybeItsJeremy
Asked:
MaybeItsJeremy
  • 2
  • 2
1 Solution
 
david_levineCommented:
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.
0
 
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.
0
 
david_levineCommented:
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.
0
 
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]
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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