Forums

  • # 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 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 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]

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

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag