【问题标题】:htaccess - unwanted redirection to index.phphtaccess - 不需要的重定向到 index.php
【发布时间】:2022-01-22 16:04:20
【问题描述】:
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=$1 [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,第二个 @ 似乎有问题987654326@.

【问题讨论】:

  • "what is the reason" - 您尝试执行这些操作的顺序,也许?
  • @CBroe - 如何重写第二个条件并保持上述顺序?
  • 为什么按这个顺序?这没什么意义。你应该总是从最具体到最不具体。如果你真的想保持这个顺序,你必须在你的第一个块中显式地添加一个异常,让/contact 的请求不变地通过。

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

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

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact)/?$ $1.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=$1 [L,QSA]

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

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact|about)/?$ $1.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=$1 [L,QSA]

【讨论】:

  • 它的工作原理非常感谢特别是contact|about部分
  • 不错的答案++兄弟
猜你喜欢
  • 2014-08-31
  • 2013-01-21
  • 1970-01-01
  • 2013-11-28
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多