Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other mod_rewrite fun

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #40805
    fooman
    Participant

    I am having a bit of trouble with some mod_rewriting.
    I have a url such as:
    http://www.example.com/about-us/deeper-link

    I’d like to convert this on the server to:
    http://www.example.com/about-us?slug=deeper-link

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^/index.php$ - [L]

    RewriteRule ^about-us/([^/.]+)/?$ about-us?slug=$1 [L]

    Now the issue is that unless I create a directory called “about-us”, I get a 500 error.
    I’d like to just have a page named about-us, not another directory.
    So I make a directory called “about-us”, add an index file in there as the page, and add a trailing slash to the rule.

    After doing that, it works.

    RewriteRule ^about-us/([^/.]+)/?$ about-us/?slug=$1 [L]

    So I guess my question is, how do I get this to work as expected without a directory… just rewrite it to a simple file…?

    #114412
    lhoezee
    Member

    You are really close..

    Try this:



    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^/index.php$ - [L]

    RewriteRule ^about-us/([^/.]+)/?$ index.php?slug=$1 [L]

    # added this line just in case someone goes to about-us without anything after it.. so you do not get a 404
    RewriteRule ^about-us/?$ index.php?slug=$1 [L]
    #114520
    lhoezee
    Member

    Sorry for the delay, I thought I would have received an email from your response..

    Usually, most of the sites I do run off of just the index file and I use mod rewrite to dynamically include other files or “content” based on the URL which then I’ll have a controller which handles template calls on the fly etc.

    If I’m understanding correctly, does this work for you?



    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f

    # Note: This will add a .php to the end of the slug value
    # example:
    # /about-us/test will actually be: /about-us?slug=test.php
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^/index.php$ - [L]

    RewriteRule ^about-us/([^/]+) about-us.php?slug=$1 [NC,L]
    RewriteRule ^about-us about-us.php [NC,L]

    That works on my side. I created an about-us.php file in my root directory and went to /about-us/test which then created /about-us?slug=test.php

    I wasn’t sure if you wanted the .php appended at the end so I added a note to one of the Rules above.

    Luke

    #114522
    lhoezee
    Member

    I found where I can get email notifications :)

    #114631
    lhoezee
    Member

    Ok, apparently even though I checked the box for emails, I didn’t get the notification of your comment and I checked in spam, strange.

    Give this a try:

    **Note: ** This assumes the first ([^/]+) is an actual static file. so for example:

    /about-us/test will actually be:
    about-us.php?slug=test

    and

    /contact-us/test2 will actually be:
    contact-us.php?slug=test2

    If the file does not exists a 404 will be generated.



    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f

    # Note: This will add a .php to the end of the slug value
    # example:
    # /about-us/test will actually be: /about-us?slug=test.php
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^/index.php$ - [L]

    RewriteRule ^([^/]+)/([^/]+) $1.php?slug=$2 [QSA,L]

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Other’ is closed to new topics and replies.