How to change page.php?setID=name to page/name/ ?

Discussion in 'Apache' started by misohoni, Oct 15, 2013.

  1. #1
    I saw nintendo's thread at the top but didn't work for me. Also if someone sets the ?setID to =anything - then no content is on the page, but the shows missing content that should be there - so I'd want it replaced with a blank page or redirect.

    Sorry guys for being so lame...
     
    Solved! View solution.
    misohoni, Oct 15, 2013 IP
  2. MBDungo

    MBDungo Active Member

    Messages:
    163
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Use these directives to rewrite "/page/$NAME/" into "/page.php?setID=$NAME":
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^page/([^/]+)/$ /page.php?setID=$1 [L]
    Code (markup):
     
    MBDungo, Oct 15, 2013 IP
  3. #3
    Rather than trying to make .htaccess turn the URI into getdata, I simply use a whitelist of files I want to have normal access, then redirect ALL requests to a single index.php thus:

    # redirect all 'unknown' requests to index.php
    RewriteEngine On
    RewriteRule !\.(gif|jpg|png|css|js|html|htm|txt|ico|zip|gz|rar|pdf|xml|wav|mp3|mp4|mpg|flv|swf|mkv|ogg|avi|webm|woff|svg|eot|ttf)$ index.php
    Code (markup):
    Then I just parse $_SERVER['REQUEST_URI'] to extract the path. I have a singleton all nice and ready to handle that:
    final class request {
    
    	private static $data = [];
    
    	public static function value($index = 0) {
    		if (count(self::$data) == 0) {
    			if (strpos(
    				$url = str_replace(['\\','%5C','/',$_SERVER['REQUEST_URI']),
    				'../'
    			)) die('Hacking Attempt Detected');
    			$offset = strlen($_SERVER['PHP_SELF'])-9;
    			if ($end = strpos($url,'?')) {
    				$end -= $offset;
    			} else $end = 4096;
    			$url = str_replace('\\','/',substr($url,$offset,$end));
    			self::$data = empty($url) ? ['default'] : explode('/',$url);
    		}
    		return isset(self::$data[$index]) ? self::$data[$index] : false;
    	}
    	
    } // request
    Code (markup):
    Default is index 0, so for example if your URI was your.url/

    request::value() == 'default'
    request::value(1) == false

    if your URI was your.url/test/goomba

    request::value() == 'test'
    request::value(0) == 'test'
    request::value(1) == 'goomba'
    request::value(2) == false

    This method also preserves getdata,so you could pass your.url/test?whiskey=tangoFoxtrot

    for the following results:
    request::value() == 'test'
    request::value(1) == false
    $_GET['whiskey'] == 'tangoFoxtrot'

    Comes out quite reliable and easy to use. Some people balk at making a large whitelist like that, but I really prefer it to trying to shove that information into getData, particularly since all the other methods erase any existing getData.
     
    deathshadow, Oct 15, 2013 IP
    misohoni and ryan_uk like this.
  4. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #4
    Thanks guys. Esp to Deathshadow but I've got 200+ files to add to the "$_GET['whiskey'] ==" request, so might be a bit complex.

    On the idea of ?setID=NotaPage, is there some PHP I could use to just show nothing ifs someone is questing a page with nothing on i.e.:

    if ($_GET["setID"] == (True?) - i.e. the exact thing in the DB then show content
    else - show nothing...
     
    misohoni, Oct 15, 2013 IP
  5. MBDungo

    MBDungo Active Member

    Messages:
    163
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    58
    #5
    About the PHP programming, you should better post a new thread about it in PHP Forum.
     
    MBDungo, Oct 15, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I think you missed how it works -- I said you can use $_GET in ADDITION to paths... You wouldn't pass them that way for simple 'files' / pages. If you had /page/name, my routine would return:

    request::value() == 'page'
    request::value(1) == 'name'

    So you could do:
    if (request::value() == 'page') {
    	if ($target = request::value(1)) {
    		if ($target != preg_replace('/[^\w]/', '', $target)) {
    			die('Invalid characters in page name.');
    		} else if (fileExists($target = 'pages/'.$target.'.page.php')) {
    			// might be good place to call template headers
    			require_once($target);
    			// might be good place to call template footers
    		}
    	} else {
    		die('no page specified');
    	}
    }
    Code (markup):
    That way "/page/test" will try to run "/pages/test.page.php", "/page/jimbo" would run "/pages/jimbo.page.php", etc, etc... You don't use get for simply loading pages at all. My method just lets you still pass getData should you want it for things like searches. (where it's nice to show the search term in the URI).
     
    deathshadow, Oct 15, 2013 IP