Advanced Search

Results 1 to 4 of 4

Thread: MOD Rewrite

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default MOD Rewrite

    Hey, I know its possible to rewrite php files to html, but how would you go about that? mod_rewrite?

    I have 2 simple types of urls..

    the 1st is just,

    www.mysite.com/this-folder/this-php-page.php

    I want that to be changed to

    www.mysite.com/this-folder/this-php-page.htm

    the other type of page is:

    http://www.mysite.com/this-folder/th...network=orange

    and I want that to be changed to

    http://www.mysite.com/this-folder/th...age-orange.htm

    Any Ideas / pushes to the right direction?

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by nikomou
    Hey, I know its possible to rewrite php files to html, but how would you go about that? mod_rewrite?
    I, for one, am not entirely sure what you're asking for here. You mention URLs later in your post, but above you're talking about files.

    The rewrite Apache module (mod_rewrite) allows a URL to be changed at request time. This may either lead to a second request from a client (a server-side redirect), or Apache might deal with the changes internally.

    What exactly do you expect to happen with this change? That the server still parses a file with a .htm extension as a PHP file? Well preferably, you should avoid file extensions altogether, and use URLs like

      http://www.example.com/some-resource

    You can then use content negotiation - either MultiViews or a type-map file - to respond with the appropriate resource.

    By the way, the .htm extension is out-dated. It was only introduced by Microsoft because their early file systems couldn't handle extensions longer than three characters. As file names on a remote Web server have no relation to a user's file system, this is irrelevant and you should use the proper .html extension instead.

    That style of manipulation - query string into file name - would need mod_rewrite, but the extension is still irrelevant.

    Code:
    RewriteEngine on
    RewriteBase path
    RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
    RewriteRule ([^.]+)\.php $1-%2.html?%1%3
    This snippet could be used in a .htaccess file to do what you described. The rewrite action would be internal, so the user would think they were going to the PHP file. You'd need to replace path with an appropriate prefix. For instance, in the directory, /deep/directory/, you'd specify /deep/directory.

    Code:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
    RewriteRule (.*/)([^.]+)\.php $1$2-%2.html?%1%3 [R]
    This is a server- or virtual-host-wide rule. Unlike the previous one, this triggers an external redirect so the user would type or navigate to an address containing an .php extension, but would see an .html extension.

    If you do want your server to treat .html (or .htm) files as PHP files, then you need to alter its main configuration (though you could use a .htaccess file or mod_rewrite). However, it would then treat all files with that extension as PHP files, whether they contained <?php ... ?> blocks or not (unless you started adding Directory directives to limit the scope).

    Mike

  3. #3
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Ok, the scenario has changed slightly now...
    my URL's will now look like something this:

    http://www.mywebsite.com/handsets.ph...set=nokia-7610
    http://www.mywebsite.com/handsets.ph...&tariff=orange

    As you can see these are very messy urls., so I want to change these to urls that look like:

    http://www.mywebsite.com/nokia-7610.html
    http://www.mywebsite.com/nokia-7610-orange.html

    Update:

    Ok, i am now using the following code for my .htaccess file, but there's a few things wrong..

    Code:
    RewriteEngine On
    RewriteRule (.*)-(.*).html handsets.php?hs=$1&tariff=$2
    OK, this works great IF there isnt a - (dash " - ") after the &tariff in the url...

    e.g. http://www.mysite.com/handsets.php?h...ariff=t-mobile. I could get rid of the dash (so it becomes &tariff=tmobile) but i want the rewrited url to have the dash in it. e.g. nokia-7610-t-mobile.html

    ALSO - if there isnt a tariff specified (handsets.php?hs=nokia-7610&tariff=) i have to place a dash " - " after the rewriten page (nokia-7610-.html)

    Anyone got any ideas?
    Last edited by nikomou; 11-10-2005 at 09:15 PM.

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Please create a new thread. Post moved to here
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •