微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

htaccess - 删除 index.php 并保留一个没有键的变量

如何解决htaccess - 删除 index.php 并保留一个没有键的变量

原始网址是以下之一:

  • example.com
  • example.com/index.PHP
  • example.com/index.PHP?c=lorem

if example.com - 什么都不应该做
if example.com/index.PHP - 它应该显示example.com,这是我的代码 - 工作正常:

RewriteEngine ON
RewriteRule ^index\.PHP/?$ https://%{HTTP_HOST}/ [NC,R=301]
RewriteRule ^/?$ index.PHP [L]

如果 example.com/index.PHP?c=lorem 它应该显示example.com/lorem

lorem 在这两种情况下都应该可以被 PHP 访问:

  • 如果用户输入 example.com/index.PHP?c=lorem
  • 用户类型 example.com/lorem

请帮忙

解决方法

您可以在站点根目录 .htaccess 中使用这些规则:

DirectoryIndex index.php
RewriteEngine On

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?c=$1 [L,QSA]
,

您能否根据显示的示例尝试以下操作。 在测试您的网址之前,请务必清除浏览器缓存。

RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
   
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=(lorem)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,NE]

##Rule for non-existing files/directories to index.php with variables.
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]


OR: 以上是专门针对 lorem 的情况,以防您的查询字符串可以是任何内容,然后尝试以下操作。

RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,L]
   
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&]*) [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,NE]

##Rule for non-existing files/directories to index.php with variables.
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。