Grow your CSS skills. Land your dream job.

.htaccess friendly url

  • # May 20, 2013 at 8:40 am

    Hi!
    I have a problem with my friendly urls.

    I want everything after mydomain.com/ should be handled as ?page=

    This is how far I’ve got so far in my .htaccess

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]*)(.*) index.php?page=$1 [NC,L]

    And it works pretty well if write /login my pagehandler catch it as **?page=login**
    but if you write an existing filename it doesn’t work.
    Example: **mydomain.com/folder** becomes **mydomain.com/folder/** (adds a slash after)
    Or if you write: **mydomain.com/file.php** it loads that php file but i want it not to.
    I want it to be handled as **?page=file.php**

    And i guess this may lead to problems linking to css, and js, if it does and you have any suggestions on how to solve that as well
    i would be very thankfull.

    I hope I’m not to confusing for you guys.
    and thanks in advance for any help you can provide.

    # May 20, 2013 at 2:38 pm

    > if you write an existing filename it doesn’t work

    Well, yeah.

    Have a look at these lines:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    # May 20, 2013 at 2:47 pm

    I think you can remove the line with -d since you probably don’t want users to browse directories like that anyway. About existing files… why would you want *mydomain.com/file.php* rewritten as *?page=file.php*? Isn’t the whole point of pretty URLS not having to use file names? You can put your php files except index.php in a private folder, so people can’t navigate to those directly.

    # May 20, 2013 at 4:03 pm

    crocodillon thats the point. i dont it want to detect files and folders. i have a class that handles the $_get variable but if i write file.php it loads that file and never gets handled by my class.

    # May 20, 2013 at 4:41 pm

    Then don’t have file.php publicly available. If your server allows you to put php files (and include them from within index.php etc.) outside the public html folder, I’d do that. If not you can use .htaccess to make a hidden directory.

    Something like:

    /domain
    /application
    all php except index.php
    /public_html
    index.php

    # May 20, 2013 at 4:56 pm

    i use

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

    and have never had problems

    but `RewriteCond %{REQUEST_FILENAME} !-f` is doing exactly what it is supposed to do if the file dose not exist then rewrite to `index.php` if you want everything to point to `index.php` then remove `RewriteCond %{REQUEST_FILENAME} !-f` and `RewriteCond %{REQUEST_FILENAME} !-d `.

    # May 20, 2013 at 5:47 pm

    > i dont it want to detect files and folders.

    Then stop telling it to. I wouldn’t recommend it, though – there may come a time when you do want files to be accessed directly.

    You could use @CrocoDillon‘s method, which is a nice solution. Only having files you want to be directly accessed in public_html makes sense. An alternative is to ensure some sort of protection is in place to prevent information being given to someone who is trying out random file names.

    Eg. include a line such as this in index.php:

    define( ‘APPLICATION’, true );

    And then every other PHP file could include something like the following:

    if( !defined( ‘APPLICATION’ ) )
    die( ‘No direct access allowed’ );

    Or, if you have a custom 404 page, you could include code in that to present it, if you don’t want them to know they hit on a valid page.

    __
    # May 20, 2013 at 9:29 pm

    > Then stop telling it to. I wouldn’t recommend it, though – there may come a time when you do want files to be accessed directly.

    For example, when you start serving scripts, stylesheets, and images. : )

    # May 21, 2013 at 6:58 am

    Thx for the help guys. I moved the files to folders and used htaccess to “protect” them.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".