12shadow12
12shadow12

Reputation: 317

htaccess - mod_rewrite pass virtual directory or file back to index.php as parameter?

I'm trying to get a virtual directory or file to be passed back to index.php

Example, I want everything after the / to get passed back to index, which will then be checked against a database, but "virtual_directory" and "virtual_file.jpg" don't actually exist, they are just parameters.

somedomain.com/virtual_directory
somedomain.com/virtual_file.jpg

Now, the problem is that EVERYTHING passes. So say I just want to go to the main directory (or just index.php) I would type in the url (somedomain.com) and it would pass the trailing slash as a parameter. It is also the same if I try to go to somedomain.com/index.php. It will pass with /index.php (ie: I cannot get to the else statement).

Is there a way to make it so it will not pass if I am directly going to the index?

These two will pass as a parameter, and I do not want them too.

somedomain.com/
somedomain.com/index.php

.htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

index.php will read what was after the '/' and do something with it.

if ($_SERVER['REQUEST_URI']) {
    echo nl2br("Parameter Passed!\n");
    echo 'Parameter That Passed:' .$_SERVER['REQUEST_URI'];
} else{
    echo 'No Parameter Passed';
    echo 'You Are Visiting domain.com/ or domain.com/index.php';
}

Upvotes: 3

Views: 712

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

The reason why the "else" part of your php code isn't being reached is because there will always be a $_SERVER['REQUEST_URI'] value. This is a server variable that will always be the request. Thus, you'll never get to "no parameter passed". What you may want instead if to either pass the parameter as a GET variable or as PATH INFO:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?request=$1 [L]

and your code would be:

if ($_GET['request']) {
    echo nl2br("Parameter Passed!\n");
    echo 'Parameter That Passed:' .$_GET['request'];
} else{
    echo 'No Parameter Passed';
    echo 'You Are Visiting domain.com/ or domain.com/index.php';
}

or pathinfo:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

and your code would be:

if ($_SERVER['PATH_INFO']) {
    echo nl2br("Parameter Passed!\n");
    echo 'Parameter That Passed:' .$_SERVER['PATH_INFO'];
} else{
    echo 'No Parameter Passed';
    echo 'You Are Visiting domain.com/ or domain.com/index.php';
}

Upvotes: 1

Jeevan Prakash
Jeevan Prakash

Reputation: 61

You can try this

 <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Upvotes: 0

Related Questions