如何在 .htaccess 文件中添加新的重写规则以针对同一文件夹中的不同文件?
How to add a new Rewrite rule in .htaccess file to target different files in the same folder?
这是我的 .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= [L]
我要添加以下内容:
RewriteRule ^([^/]+)$ /category.php?category= [L]
在末尾添加这一行会产生服务器错误,因为它们匹配相同。这两条规则都旨在改变同一文件夹中不同文件的URL。
我要的是打下面link
website.com/y
y 可以加载的地方 category.php?category=category1
还有
website.com/x
x 可以加载的地方 article.php?slugId=some-slug
article.php 和 category.php 将是同一文件夹中的两个不同文件。
如何更改规则以使其生效?
能否请您尝试按照您显示的示例编写。
在以下解决方案中:
- 请将
firststring
更改为您要在 URI 中查找的字符串,同时在后端将其重写为 article.php
。
- 将
secondstring
更改为要在 URI 中查找的字符串,同时将其重写为 category.php
。
Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{REQUEST_URI} firststring [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId= [L]
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} secondstring [NC]
RewriteRule ^([^/]+)/?$ category.php?category= [L]
这是我的 .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= [L]
我要添加以下内容:
RewriteRule ^([^/]+)$ /category.php?category= [L]
在末尾添加这一行会产生服务器错误,因为它们匹配相同。这两条规则都旨在改变同一文件夹中不同文件的URL。
我要的是打下面link
website.com/y
y 可以加载的地方 category.php?category=category1
还有
website.com/x
x 可以加载的地方 article.php?slugId=some-slug
article.php 和 category.php 将是同一文件夹中的两个不同文件。
如何更改规则以使其生效?
能否请您尝试按照您显示的示例编写。
在以下解决方案中:
- 请将
firststring
更改为您要在 URI 中查找的字符串,同时在后端将其重写为article.php
。 - 将
secondstring
更改为要在 URI 中查找的字符串,同时将其重写为category.php
。
Options +FollowSymLinks -MultiViews
AuthType Basic
AuthName "website"
AuthUserFile ""
require valid-user
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{REQUEST_URI} firststring [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId= [L]
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} secondstring [NC]
RewriteRule ^([^/]+)/?$ category.php?category= [L]