Any .htaccess gurus here?

  • I am working on my own personal site to make my urls more SEO compliant:


    I have links that are like:


    http://domain.tld/index.php?nav=social


    That can be reduced to:


    http://domain.tld/?nav=social


    And after I added:


    Apache Configuration
    #remove .php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    #for pretty url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ index.php?nav=$1 [NC,L]

    To my .htaccess file, I can now access the file at:


    http://domain.tld/social



    Now for my real question:


    I have links like:


    http://domain.tld/?nav=footer&id=staff


    and


    http://domain.tld/?nav=games&id=gametype&page=pagename


    How can I reduce them to:


    "http://domain.tld/staff" and "http://domain.tld/games/gametype/pagename" respectively


    Thank you for your help


    :)


    I would really love an answer so if Alexander Ebert or Tim Düsterhus feel like answering I would be super happy.

    • Official Post
    Apache Configuration
    RewriteRule ^/?([^/]+)(?:/([^/]+)(?:/([^/]+))?)?$ index.php?nav=$1&id=$2&page=$3

    That should work and is basically just splitting the path into three segments (last two are optional) separated by forward slashes. You could consider rewriting the path to feed it into the PHP file and just evaluate it inside PHP, which is exactly what we're doing in our software.

  • So to this?


    Apache Configuration
    #remove .php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    #for pretty url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ index.php?nav=$1 [NC,L]
    RewriteRule ^/?([^/]+)(?:/([^/]+)(?:/([^/]+))?)?$ index.php?nav=$1&id=$2&page=$3
  • Apache Configuration
    #remove .php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    #for pretty url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/?([^/]+)(?:/([^/]+)(?:/([^/]+))?)?$ index.php?nav=$1&id=$2&page=$3 [NC,L]

    So this is what I have


    I tested one of those URLs and it looks like a stripped out page with no CSS, was just curious.

    • Official Post

    I fixed it by adding a foward slash to my css script

    Yes, this is required because the browser interprets your fancy urls as directories. Any resource that is included using relative paths (such as without slashes) is interpreted to be relative to the fake directories in the link, thus you'll need to include these files explicitly with a leading slash to tell the browser exactly where to look at.

  • One last question then.....


    If some of my pages where something else on the third part


    &pages


    is


    &somethingelse


    Is it possible to have both?

    • Official Post

    No, because the webserver is unable to tell those apart, /a/b/c/ and /a/d/c/ simply look the same if you get down to the structure.


    If you want to interpret the URL differently depending on which page you are, then I suggest you capture everything and have it evaluated via PHP. This is also the exact reason why we do it this way ;)

    • Official Post

    The .htaccess performs just pattern matching, it couldn't care less about what it is matching. The regular expression provided by me matches on anything that is enclosed between the slashes, including but not limited to dashes. How it is resolved in PHP is entirely up to you, that is outside of the domain of the .htaccess.

  • These are modifiers for the rule. NC is "no case", i.e. ignore upper and lowercases.

    L is "last rule", that means, if that rule applies, ignore every rule that comes after it. Think of it like a return command in a script.


    There are a lot more, you might also find R=XXX a lot - here is a list of what's possible: https://httpd.apache.org/docs/2.4/rewrite/flags.html

    Milestones:

    • 18.02.2022 19:14 CET: Erste PWA installiert (und es war ausgerechnet YouTube Music)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!