Trying to Tweak Rewrite Rule to Exclude URLs with Certain Extensions

Discussion in 'Apache' started by Darden12, Oct 15, 2008.

  1. #1
    I'm an apache newbie who has finally got a rewrite rule working, namely:

    RewriteRule (.*)$ index.php?FixedTitle=$1

    The problem is, it's working too well because it's rewriting literally everything, including my image files.

    How can I alter the above rule so that it will only affect URLs of the following form, which, as you see, have no extensions:

    mydomain.com/ARTICLE_NAME1
    mydomain.com/ARITCLE_NAME2

    The above are the only files that I want rewritten by the rule. (I don't want files ending in .html .jpg .php (etc.) to be rewritten too.)

    Any ideas on what change I need to make in the rewrite rule?

    Thanks!

    Brian
     
    Darden12, Oct 15, 2008 IP
  2. seismic2

    seismic2 Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This should do it :
    RewriteRule ^ARTICLE_NAME([0-9]+)$ index.php?FixedTitle=$1
    Code (markup):
     
    seismic2, Oct 16, 2008 IP
  3. Darden12

    Darden12 Well-Known Member

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thanks very much for the answer, Seismic -- I'm afraid that I didn't explain the problem clearly enough, however. I should have taken a little more time on my description.

    In reality, there are no files involved that actually have the names ARTICLE_NAME1, ARTICLE_NAME2, and so forth.

    That was just my confusing way of saying that I have a long list of article names.

    In other words, the actual addresses that I DO want to rewrite look like this:

    The-Big-Box
    War-of-the-Worlds
    Now-Hear-This

    and so forth.

    The only thing these addresses have in common is that they have no file extensions, as opposed to the file names that I DON'T want to rewrite, which end in .html, .php, jpg, gif, and so forth.

    My question is, how to rewrite my extension-free addresses without also rewriting the file names WITH extensions.

    Any ideas, now that I (hopefully) have stated the case more clearly?
     
    Darden12, Oct 16, 2008 IP
  4. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What you want is a RewriteCond.
    RewriteCond %{REQUEST_URI} !(\.jpg|\.html|\.php)
    RewriteRule (.*)$ index.php?FixedTitle=$1
    Code (markup):
    What this does is applies the condition in the RewriteCond before the rewrite rule. The RewriteRule is only executed if the RewriteCond matches.

    So, the URL has to not have ".jpg" or ".html" or ".php" in it for the RewriteRule to be executed.


    If you only want to avoid rewriting URLs that end in these extensions then you can use this one instead:
    RewriteCond %{REQUEST_URI} !(\.jpg|\.html|\.php)$
    RewriteRule (.*)$ index.php?FixedTitle=$1
    Code (markup):
     
    Ladadadada, Oct 16, 2008 IP