简体   繁体   English

htaccess - mod_rewrite将虚拟目录或文件作为参数传递回index.php?

[英]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 我正在尝试将虚拟目录或文件传递回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. 例如,我希望在/之后将所有内容传递回索引,然后将对数据库进行检查,但“virtual_directory”和“virtual_file.jpg”实际上并不存在,它们只是参数。

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. 所以说我只想进入主目录(或只是index.php)我会输入url(somedomain.com),它会将尾随斜杠作为参数传递。 It is also the same if I try to go to somedomain.com/index.php. 如果我尝试去somedomain.com/index.php也是一样的。 It will pass with /index.php (ie: I cannot get to the else statement). 它将与/index.php一起传递(即:我无法访问else语句)。

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: 的.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. index.php将读取'/'之后的内容并对其执行操作。

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';
}

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. 你的PHP代码的“else”部分未被访问的原因是因为总会$_SERVER['REQUEST_URI']值。 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: 如果要将参数作为GET变量或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: 或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';
}

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM