Grow your CSS skills. Land your dream job.

mod_rewrite fun

  • # November 14, 2012 at 3:16 pm

    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…?

    # November 15, 2012 at 7:36 am

    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]
    # November 15, 2012 at 9:37 am

    Thanks for your help!

    Your rules are for a single-page site (physically just an index file) and all content is derived from the URLs… correct?

    I am actually going to have an about-us.php file. Different layout and whatnot.
    I can only get the rule to work, however, if I create a directory called “about-us”. Rather than just have a php file with that name.

    So I’d like to do this with an ‘about-us.php’ file:

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

    But I get a good ol’ 500 error.

    I apologize if I’m confusing things. Hard to explain a bit online :(

    # November 15, 2012 at 11:34 pm

    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

    # November 15, 2012 at 11:42 pm

    I found where I can get email notifications :)

    # November 16, 2012 at 10:56 am

    I will try this! Thanks so much for your help.

    And I also need to find the email notification option haha
    Thx for pointing out it’s there.

    # November 16, 2012 at 11:15 am

    Ok so after looking at your example, that will definitely work.
    What if you want a rewrite rule that will work for any page and not just about-us?

    Do you have to make separate rules for all of the pages/sections of your site? Or can you use a ([^/]+) variable and serve the page based on that?

    # November 17, 2012 at 9:25 pm

    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]

    # November 19, 2012 at 12:04 am

    Thanks for the update! I had tried something almost exactly like this, however I did not use .php at the end of the rule because I thought the top-most conditions made it so that the code did not require a php extension at the end of the rules I wrote.

    I will surely try this out tomorrow at work. Thank you!

    Regarding the email notifications. The forum can use a bit of work in regards to the fact that you get a notification after every single reply on a thread you bookmark or participate it. So even if you haven’t visited a thread and there was 25 responses, you will get 25 emails. That is a bit lame, but I’m sure others feel more positive about that lil’ issue than I do. It just fills up my inbox when I’m not on a computer.

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".