SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Location
    California, USA
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Just Can't Figure It Out

    I want to change "index.php?page=page2" to "page2.php" using .htaccess. How could this be accomplished?

    I have tried the following, but to no avail.

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    ErrorDocument 404 404.php
    
    RewriteRule ^(.*).php$ index.php?page=$1
    Also, my 404 error doesn't work; it just displays raw text of "./404.php" instead of redirecting me to the 404 page.
    Help would be magnificently appreciated.

  2. #2
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,892
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Sheegoth View Post
    I want to change "index.php?page=page2" to "page2.php" using .htaccess. How could this be accomplished?

    I have tried the following, but to no avail.

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    ErrorDocument 404 404.php
    
    RewriteRule ^(.*).php$ index.php?page=$1
    Also, my 404 error doesn't work; it just displays raw text of "./404.php" instead of redirecting me to the 404 page.
    Help would be magnificently appreciated.
    As I understand it, commands in .htaccess only work on the file name and path, and not on any query parameters. So as far as .htaccess is concerned, you've referenced index.php, not index.php?page=page2, because it ignores the query parameters.

    Try putting /404.php for the error document. That seems to work for me!

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Location
    California, USA
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the timely reply.

    I managed to achieve what I wanted with the following code.

    Code:
    RewriteRule ^(.*).php$ index.php?page=$1 [L,QSA]
    Unfortunately however, my attempts at altering the 404 page has proven futile as of late.

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

    First, you've got to get the syntax correct for ErrorDocument to work - and that REQUIRES the redirection to be absolute (internal or external) so all you need is a preceding / or http://example.com/404.php. Of course, it helps if you have a 404.php script in your DocumentRoot.

    Finally, why do you not expect (.*)\.php to match your index.php redirection (and loop until it gets tired of looping)? Please have a read about (.*) in my signature's tutorial and why it's so BAD to use nearly all the time (and especially in this case).

    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
  •