用 .htaccess url rewrite 重写不同的页面

Rewrite different page with .htaccess url rewrite

我想使用 .htaccess 进行 url 重写。 在我之前的 中,您向我展示了如何对 index.php 页面执行此操作,但现在也想对 browser.error.php 等其他页面执行此操作。 为此,我尝试在 .htaccess 中添加最后两行,但不幸的是,如果我尝试 link www.mysite/browser-error/ 它不起作用 - Return me in the index页。

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=&idric=&sp= [L,QSA,NC]
RewriteRule ^(.*)/(.*)\/?$ index.php?op=&idric= [L,QSA,NC]
RewriteRule ^(.*)\/?$ index.php?op= [L,QSA]

#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r= [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]

我哪里做错了?我怎么去?谢谢

在这条规则之前你有规则。这会使您的请愿书在达到您的规则之前更改为 index.php。 (它们都有 L 标志,使它们成为最后处理的规则)。由于它们不限于在 url 是浏览器错误时不应用,因此它们将在每个 ///* 或类似请求上 运行。

然后,您应该在其他 RewriteRules 中添加 RewriteCond 来消除浏览器错误 url,如下所示:

请尝试,如果这不是您需要的,请发表评论,如果它没有按预期工作。

希望对您有所帮助

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=&idric=&sp= [L,QSA,NC]

RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)/(.*)\/?$ index.php?op=&idric= [L,QSA,NC]

RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)\/?$ index.php?op= [L,QSA]

#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r= [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]

我建议避免使用 .* 并使用 [^/]+ 来重新排序您的规则:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#NEW ADDED ROWS
RewriteRule ^browser-error/([^/]+)/?$ browser.error.php?r= [L,QSA,NC]
RewriteRule ^browser-error/?$ browser.error.php [L,NC]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?op=&idric=&sp= [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?op=&idric= [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?op= [L,QSA]