重写规则页面类别问题
我有以下 .htaccess 重写 url,例如 url.com/category/page 和 url.com/page,然后 PHP 设置 $_GET 中的变量。
问题是页面category.php仅适用于url.com/category,但是当我想要转到url.com/category/page时它会失败,因为文件category.php和文件夹类别需要存在并且因为文件夹存在它不起作用...我不确定这是否有意义,但我真的找不到解决这个问题的方法
..htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
PHP
$url = '';
if (!empty($_GET['category'])) {
$url .= $_GET['category'] . '/';
}
if (!empty($_GET['page'])) {
$url .= $_GET['page'] . '.php';
} else {
$url .= "pages/home.php";
}
include $url;
I have the following .htaccess rewriting urls such as url.com/category/page and url.com/page and then the PHP to set the variables from the $_GET's.
The issue is that the page category.php will work with just url.com/category but then when i want to go url.com/category/page it fails because the file category.php AND folder category need to exists and because the folder exists it does not work... i am unsure if this makes sense but i am really failing to see a way around this..
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
PHP
$url = '';
if (!empty($_GET['category'])) {
$url .= $_GET['category'] . '/';
}
if (!empty($_GET['page'])) {
$url .= $_GET['page'] . '.php';
} else {
$url .= "pages/home.php";
}
include $url;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一个条件,该条件会将浏览器重定向到它存在的文件夹
(
RewriteCond %{REQUEST_FILENAME} !-d
)。如果你删除它,它应该可以工作。如果您想有条件地将其重定向到文件夹(如果存在),您可以使用 PHP 中的 if 语句进行调整。
You have a condition in your code which will redirect the browser to the folder it it exists
(
RewriteCond %{REQUEST_FILENAME} !-d
). If you remove this, it should be working.If you would like to redirect it conditionally to a folder if it exists, you could adjust it using if statements in PHP.