Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在索引页上重写URL_Php_.htaccess_Url Rewriting - Fatal编程技术网

Php 在索引页上重写URL

Php 在索引页上重写URL,php,.htaccess,url-rewriting,Php,.htaccess,Url Rewriting,我有一个网站*。我想动态打开主页、类别和子类别,如: https://www.example.com https://www.example.com/education https://www.example.com/education/PHP-Training 我编写了index.php,如下所示: if(isset($_GET['category'])){ $category = $_GET['category']; $subCategory = $_GET

我有一个网站*。我想动态打开主页、类别和子类别,如:

https://www.example.com
https://www.example.com/education
https://www.example.com/education/PHP-Training
我编写了index.php,如下所示:

if(isset($_GET['category'])){
        $category = $_GET['category'];
        $subCategory = $_GET['subcategory'];
        include('files/categoryFile.php');
    }else{ 
        include('files/indexFile.php');
    }
categoryFile.php
将使用上述两个变量
$category
$subCategory
从数据库获取数据,
indexFile.php
是索引页面的静态文件。 下面是重写规则执行相同操作的
.htaccess
代码:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?category=$1 [NC,L]
它在这里工作正常,但问题是,它也重写
https://www.example.com/css/style.css
使用相同的规则。我可以通过使用以下代码将
.htaccess
文件拖放到此文件夹来阻止它重写这些特定文件夹:

<IfModule mod_rewrite.c>

#Disable rewriting
RewriteEngine Off

</IfModule>

#禁用重写
发动机熄火

但对我来说,这不是一个好的解决方案。我想知道是否有更好的解决办法。如果此问题与问题标准不匹配。请评论,我将删除该问题。此外,如果可能有更改,建议进行编辑。谢谢

我在我的一个项目中做过这件事。您必须在重写规则之前添加一个条件,另外一件事是您应该创建资产文件夹,以便您可以在其中存储所有公共文件和文件夹,并且可以使用一个规则公开

根据您的示例检查此解决方案

    RewriteCond %{REQUEST_URI} !/css
    RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) index.php?category=$1&subcategory=$2 [NC,L]
    RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?category=$1 [NC,L]
我认为您只需要再添加几个条件来排除它们

RewriteCond %{REQUEST_URI} !/css
RewriteCond %{REQUEST_URI} !/js
RewriteCond %{REQUEST_URI} !/images 

使用重写条件忽略特定文件或文件类型我已经尝试过了,但没有成功。它只是忽略了文件类型规则@Bernhard我将如何向不重写列表添加更多文件夹
%{REQUEST\u URI}/css、/images、/js、/folder
%{REQUEST\u URI}/(图像| css | js |文件夹)$[NC]
。我很抱歉。我是.htaccess的新手,您可以这样添加RewriteCond%{REQUEST_URI}^/客户端/RewriteCond%{REQUEST_URI}^/somefolder/不同文件夹的不同行?是的,你是对的。检查我上面的答案,我已经更新了。@PrashantRajpoot如果问题解决了,那么你可以勾选为正确答案。