SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry .htaccess RewriteRule problem

    Hi

    I am trying to setup my URL rewrites to check for a certain template and if it doesnt exist use a default one

    So if I have a URL:
    http://www.mysite.co.uk/feed/

    I want it to check if feed.php exists and use that if it does, but if it doesn't to use page.php?page=feed instead

    This is what I currently have:
    Code:
    Options +FollowSymlinks
    RewriteEngine On
    
    RewriteRule ^([^/.]+)/$ $1.php [L]
    RewriteRule ^([^/.]+)/$ page.php?page=$1 [L]
    
    
    ErrorDocument 404 /404.php
    It works fine if feed.php exists, but when it doesn't it defaults straight to my custom 404.php

    I know I can specify RewriteRule ^feed/$ feed.php [L] and have a catchall for page.php, but I will have a lot of these and do not want to have to hard code them all

    Any help would be much appreciated! =D

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    sm,

    You've got a GREAT start to coding mod_rewrite but simply didn't translate your words into code properly.

    Quote Originally Posted by sensationmm View Post
    I want it to check if feed.php exists and use that if it does, but if it doesn't to use page.php?page=feed instead
    Code:
    RewriteEngine on
    
    # Check if xyz/ exists as xyz.pdf and redirect
    RewriteCond /$1.pdf -f
    RewriteRule ([a-z]+)/ $1.php [L]
    # Note, this changes the directory level so you'll have problems with relative links
    
    # Otherwise redirect xyz to page.php as the value of page
    RewriteCond /$1.pdf !-f
    RewriteRule ([a-z]+)/ page.php?page=$1 [L]
    # Same directory level change so same problem
    
    # That's why I loathe trailing /'s on filenames
    Alternatively, MultiViews will attempt the first redirection for you automatically. MultiViews is a horrid function which, because it hijacks a directory name and attempts to serve a file, it causes major problems for the unsuspecting. IMHO, do it properly with mod_rewrite so you know everything that's going on with your redirections.

    Oh, for your code:

    Code:
    Options +FollowSymlinks
    # That should already be in the httpd.conf so it's redundant (multiple times for EVERY request!)
    
    RewriteEngine On
    
    RewriteRule ^([^/.]+)/$ $1.php [L]
    RewriteRule ^([^/.]+)/$ page.php?page=$1 [L]
    # If the first RewriteRule matches and redirects, how will the second RewriteRule ever match?
    # Clearly it won't so the check on the existence of the file (with/without the .php extension)
    # will separate the two rules.
    
    ErrorDocument 404 /404.php 
    # Because mod_rewrite is not an Apache core module, it's always executed after all the core
    # directives and modules (e.g., mod_alias) SO I group the core and core module directives
    # at the top of my .htaccess file
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •