htaccess - 不需要的重定向到 index.php

htaccess - unwanted redirection to index.php

RewriteEngine ON

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteRule ^([^/.]+)/?$ index.php?c= [L,QSA]

上面的工作正常,现在我想在内部 example.com/contact 重写成 example.com/contact.php 我试过的地方:

# contact -> contact.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^contact$ /contact.php? [L]

问题 - 如果我删除第一段代码(大约 index.php),这会将我重定向到 index.php - 第二部分工作正常 - 没有重定向到 index.php,第二个 RewriteCond.

似乎有问题

使用您显示的示例,请尝试遵循 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact)/?$ .php [QSA,NC,L]

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?\?c=(\S+)\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

##For internal rewrite rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c= [L,QSA]

OR 如果您必须在内部重写为 about to about.php 或联系 contact.php 那么您可以使用以下规则。

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact|about)/?$ .php [QSA,NC,L]

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?\?c=(\S+)\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

##For internal rewrite rules here.    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c= [L,QSA]