views:

49

answers:

3

I want to set a default variable for a query that comes at the end of a url

The .htaccess file redirects the url as follows:

http://www.server.com/folder/subfolder/index.php?page="some-page-name"

displayed url

http://www.server.com/folder/some-page-name


if the page name is not set, as in:

http://www.server.com/folder/

the default would be "index". I could use php function header("location:url") but that would display the "index" part at the end if the url... that I don't want.



htacess content

   Options -Indexes

    <IfModule mod_rewrite.c>
    RewriteEngine On
    #RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
    RewriteBase /folder/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
    RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L]
    </IfModule>

    <IfModule mod_rewrite.c> 
    ErrorDocument 404 /error.html
    ErrorDocument 403 /error.html
    </IfModule>
+1  A: 

You don't have to redirect to index.php. You can use something like:

header('Location: /folder/front-page');

If you just want http://example.com/folder/ show up your index page, you could use the following in your PHP script:

$requested_page = filter_input(INPUT_GET, 'pageID');
$allowed_pages = array('some-page', 'some-page-name');
if($requested_page == ''){
   // display your index page as ?pageID is not set or empty
}
elseif(in_array($requested_page, $allowed_pages)){
   // display $requested_page
}
else{
   // display a 404 Not Found error
}
Lekensteyn
For this to work he'd have to do `header('Location: /folder/index');` as "front-page" isn't his default page, "index" is, and he specifically said "that would display the "index" part at the end if the url... that I dont' want".
Josh
I misunderstood his question then, I thought he was talking about `/index.php?pageID=front-page`. Revisited my answer.
Lekensteyn
@Lekensteyn, thank you very much!!!
dany
A: 

Your question was too long, didn't read, however if you just want to set a variable in your .htaccess file and pass it to PHP it's pretty easy to do it like this -

In the .htaccess file (http://httpd.apache.org/docs/2.0/mod/mod_env.html#setenv):

SetEnv default_url http://my.url/goes/here

In your PHP script (http://www.php.net/manual/en/reserved.variables.environment.php):

header('Location: '. $_ENV['default_url']);

Alternately, if you have ErrorDocument handlers, you might be able to simply send a status code as well (see the third option to header(), http://us.php.net/manual/en/function.header.php).

Dan Beam
I don't think that's what he's asking... maybe reading his question would have helped?
Josh
+1  A: 

Try this code in your PHP file:

$pageID = 'index';

if(isset($_REQUEST['pageID']) && !empty($_REQUEST['pageID']))
    $pageID = get_magic_quotes_gpc() ? stripslashes($_REQUEST['pageID']) : $_REQUEST['pageID'];

// Your code should now use the $pageID variable...

What this will do is set $pageID to a default value of "index". Then, if a different PageID is given by the mod_rewrite, $pageID will bet set to that value. Your code should then use the value of $pageID.

Josh
@Josh, thanks, thanks alot!!!
dany