如何删除" /index.php?"," .php",并强制尾随斜杠" /"在URL? (htaccess重写规则)

时间:2014-03-29 18:22:44

标签: .htaccess mod-rewrite redirect

我们在public_html的新目录中有一个新网站,如下图黄色所示。 我们需要向new_site/.htaccess添加规则以满足以下三条规则:

  1. 从网址末尾删除.php
  2. 在大多数网址的末尾强制尾随斜杠/(不包括.jpg,.png,...)
  3. 从各种网址中删除/index.php?
  4. enter image description here

    最有可能的是,这就是我试图提出的问题:

    目前,

    domain.com/ns/abc.php
    domain.com/ns/abc_abc.php
    domain.com/ns/index.php?/abc
    domain.com/ns/abc/index.php?/abc
    domain.com/ns/abc/abc/index.php?/abc/123
    domain.com/ns/abc/abc/index.php?/abc/abc/123/123
    

    domain.com/ns/abc/
    domain.com/ns/abc_abc/
    domain.com/ns/abc/
    domain.com/ns/abc/abc/
    domain.com/ns/abc/abc/abc/123/
    domain.com/ns/abc/abc/abc/abc/123/123/
    

    我们还尝试了一些以前发布的规则来执行此任务。如

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    RewriteBase /ns
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ %1 [R=301,L]
    

    上述代码帮助我们删除了index.php。但是,我们无法弄明白如何删除/index.php?

    任何评论/建议表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:1)

以下内容应该可以实现这一目标:

RewriteEngine On
RewriteRule ^ns/(.*)/(.*)/index.php/(.*)/(.*)/(.*)/(.*)$ /ns/$1/$2/$3/$4/$5/$6/ [R,L]
RewriteRule ^ns/(.*)/(.*)/index.php/(.*)/(.*)$ /ns/$1/$2/$3/$4/ [R,L]
RewriteRule ^ns/(.*)/index.php/(.*)$ /ns/$1/$2/ [R,L]
RewriteRule ^ns/index.php/(.*)$ /ns/$1/ [R,L]
RewriteRule ^ns/(.*).php$ /ns/$1/ [R,L]

编辑: 第二次尝试:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/(.*)/(.*)/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2/%3/%4\? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/(.*)/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2/%3\? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2\? [R,L]
RewriteRule ^ns/(.*).php$ /ns/$1/ [R,L]