Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Apache 如何在.htaccess文件中添加新的重写规则以针对同一文件夹中的不同文件?_Apache_.htaccess_Mod Rewrite_Url Rewriting_Friendly Url - Fatal编程技术网

Apache 如何在.htaccess文件中添加新的重写规则以针对同一文件夹中的不同文件?

Apache 如何在.htaccess文件中添加新的重写规则以针对同一文件夹中的不同文件?,apache,.htaccess,mod-rewrite,url-rewriting,friendly-url,Apache,.htaccess,Mod Rewrite,Url Rewriting,Friendly Url,这是我的.htaccess文件: Options +FollowSymLinks -MultiViews AuthType Basic AuthName "website" AuthUserFile "" require valid-user RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^

这是我的.htaccess文件:

Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /article.php?slugId=$1 [L]
我想补充以下几点:

RewriteRule ^([^/]+)$ /category.php?category=$1 [L]
在末尾添加这一行会产生一个服务器错误,因为它们都匹配相同。这两条规则都旨在更改同一文件夹中不同文件的URL

我想要的是点击下面的链接

website.com/y
y可以在哪里加载category.php?category=category1

还有

website.com/x
x可以在哪里加载article.php?slugId=some slug

php和category.php将是同一文件夹中的两个不同文件


如何更改规则以使其正常工作?

请尝试以下内容,并按照所示示例编写

在以下解决方案中:

  • 请将
    firststring
    更改为您希望在URI中查找的字符串,同时在后端将其重写为
    article.php
  • secondstring
    更改为要在URI中查找的字符串,同时将其重写为
    category.php

很明显,它会给您一个内部服务器错误,因为您已经存在规则。请告诉我们您希望通过
article.php
重写的URI的条件/类型,以及您希望将其写入
category.php
的URI,然后只有我们才能修复它,如果不知道在什么情况下应该调用哪个规则,那么就不可能实现它。@RavinderSingh13我想,如果我将文件更改为不同的文件夹,然后分别更改规则以匹配每个文件夹的名称,那么它就可以工作了?那么你的意思是说像这样的url
http://localhost:80/test1
点击它应该由
http://localhost:80/singh_file_backend.php
有人点击了
http://localhost:80/test2
应由
http://localhost:80/singh_test2_file_backend.php
?“是这样吗?@RavinderSingh13没错,我已经把问题改清楚了,谢谢。好了,现在开始工作了。这就是我写的(如果以后有人看到的话):RewriteCond%{REQUEST_FILENAME}-已重写cond%{DOCUMENT_ROOT}/$1-f RewriteCond%{REQUEST_URI}/category1 | category2 | category3 | category4/[NC]RewriteRule^([^/]+)/?$category.php?category=$1[L]RewriteCond%{DOCUMENT_ROOT}/$1-f RewriteCond%{REQUEST_FILENAME}-d RewriteCond%{REQUEST_URI}(.*)[NC]RewriteRule^([^/]+)/?$article.php?slugId=$1[L]@MiguelCardonaPolo,做得很好,你可以通过添加这个来编辑我的答案,我会批准你的编辑,然后干杯。为了让未来的用户能够从中受益,我将批准您的编辑欢呼和愉快的学习
Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} firststring [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId=$1 [L]


RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} secondstring [NC]
RewriteRule ^([^/]+)/?$ category.php?category=$1 [L]