SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    0 Post(s)
    0 Thread(s)

    Problem with URL rewriting

    Lately i have been using a htaccess code to change my site file names from e.g. localhost/tutorials/rewrite/about.php to localhost/tutorials/rewrite/about/
    As you can see i am using my xampp localhost and the htaccess file is located in the rewrite folder

    The code used works well. Use request_filename to make sure its not a file or folder first and then do the busniess
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1.php [L]

    The problem is that when making a link for a page. I have changing it to
    The htaccess code did work when i didnt have the first to change the .php to /

    RewriteRule ^events/([^/]*)/$ events/?url=$1 [L]
    But with the first rule in place my page link which should read <a href="events/event-namer-tile/"> becomes <a href="localhost/tutorials/rewrite/localhost/tutorials/rewrite/events/event-name/">

    So each works on there own but breaks when brought together

    Any help please

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    19 Post(s)
    3 Thread(s)

    As for your trailing /, IMHO, that's VERY bad as that is telling your visitor that your request is one level deeper in the file structure than it really is (making relative links miss the requested file by one folder level),

    Then, your first mod_rewrite block is not changing the query string so there should be no impact.

    Finally, the problem is that you're also making the new link's query string into a filename. How is Apache supposed to know whether it's a file or query string?

    Recommendation: Get rid of the trailing / and turn events.php?name=event-name into events?name=event-name.

    It's all simple logic - have a read of the tutorial linked in my signature.


    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


Posting Permissions

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