SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A basic rewrite is giving me a 404 page

    Hey all,

    I'm new to rewriting.
    I'm trying to get a url to split into parameters to pass to php.

    The rewrite looks like this.

    Code:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    
    RewriteRule ^(.*)$ index.php?id=$l [QSA, L]
    But when I browse to www.site.com/testing I get an error.

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, webadmin@kundenserver.de and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
    This is a shared hosting package. I have WordPress working on one of the other domains perfectly. This uses rewrites so I'm fairly sure it's enabled.

    Any help would be great, thanks.

  2. #2
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I have got a bit further but now I have a 404 page rather than an error 500.

    My modified htaccess is

    Code:
    AddType x-mapp-php5 .php
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    
    RewriteRule ^v/([^/]*)$ index.php?id=$1 [L]

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,118
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Let's start really basic, so we can get an idea of where you are and where you want to be.

    First give me an example of the URL as it exists today, then show me what you want it to look like.

    Once we know those two items, we can get you going in the right direction.

  4. #4
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for that

    I have been tweaking all day and at the moment I have a page set up at

    www.barrasweb.co.uk/hello

    Which redirects to barrasweb.co.uk/index.php?id=

    I'd prefer it to rewrite rather than redirect but its a start.

    the htaccess file is as below. There is a load of junk and comments in there where I have been working.

    Code:
    AddType x-mapp-php5 .php
    
    Options +FollowSymLinks
    RewriteEngine On
    #RewriteBase /
    
    #RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteCond %{REQUEST_FILENAME} !-l
    
    #RewriteRule ^(.*)$ index.php?id=$l [QSA, L]  
    #RewriteRule ^([a-z0-9\-]+)$ index.php?id=$l [QSA, L]  
    ##RewriteRule ^v/([^/]*)$ index.php?id=$1 [L]
    ##RewriteRule ^v/hello.html landed.html [L]
    ### WORKING RewriteRule ^hello\.html$ /landed.html [R,NC,L]
    
    ### STILL WORKS RewriteRule ^hello$ /index.php?id=$1 [R,NC,L]
    
    RewriteRule ^hello$ /index.php?id=$1 [R,NC,L]
    The last line in there seems to work but obviously it's only looking for a static hello page.

    In the end I'd like

    /folder/page

    to redwrite to

    /index.php?id=folder/page

    Thanks in advance.

  5. #5
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, still tinkering.

    I have just changed the htaccess file to read

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index\.php)$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php?id=$1 [L]
    This seems to work but it's not passing anything to the GET super global.

    Thanks again

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,118
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, so you basically want to do a catch-all and send it to index.php under the variable id.

    Code:
    RewriteEngine On
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?id=$1 [L]
    I typically don't recommend catch-alls, but I also understand why one may want to control it pragmatically where they feel they have better control over the processing versus creating different handler files and having apache capture the data and give it to the appropriate handler. Two different methods that ultimately achieve the same end goal.

    The above should capture the information and pass it into the id variable.

  7. #7
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Absolutely fantastic, thanks for that.

    Works perfectly. I just wish I knew what was going on.

    I think I get most of it except the two RewriteRules. I understand the concept of the second one but I'm not sure what the first one is doing.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,118
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Well, the first one simply states, if the request is for index.php leave it alone and just let it process index.php.

    The second says, capture everything, store it in $1 and pass to index.php in the variable id

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    An explanation for both of you:

    1. It's ALWAYS dangerous to use (.*) BECAUSE it does capture EVERYTHING (or NOTHING) so any hiccup in your website will be caused by this inappropriate use of (.*). This is explained in my signature's tutorial as well as ways to correctly specify what you WANT to match ('hello' in this case).

    2. The {REQUEST_FILENAME} lines were looking to NOT match a file (!-f), directory (!-d) or symbolic link (!-l). I have no idea why you'd want to involve a symbolic link but ... well, the other two are fine for the generic case (where you have no idea what the URI will be).

    3. The {REQUEST_URI} used to exclude index\.php was used to escape from the infinite loop of the EVERYTHING atom, (.*). Of course, as you add images and scripts, they'll have to be added to the list of excluded files (surprise! That's why you should NEVER use (.*) unless you actually know what you're doing.

    4. Kudos to cp for showing you how to create Apache variables (using the parenthetical in a mod_rewrite statement). More on that in my signature's tutorial, too.

    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


Tags for this Thread

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
  •