Remove/Redirect public/index.php 来自 url 以防止重复 url

Remove/Redirect public/index.php from url to prevent duplicate urls

您好,我遇到了从 url 中删除 public/index.php 的问题。 Remove/Redirect index.php from url to prevent duplicate urls 这个 link 确实帮助我从 url 中删除 index.php,但我无法从 url 中删除 public/index.php。这是我下面的 htacces 代码

 RewriteEngine On
 #REmove index.php from url starts
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php[^/] /? [L,R=302,NC,NE]

    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php(?:/(.*))?$ /? [L,R=302,NC,NE]
 #Remove index.php from url ends

   RewriteCond %{HTTPS} off
   RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

   RewriteCond %{HTTP_HOST} !^www\. [NC]
   RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 <IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$    public/    [L]
   RewriteRule    (.*) public/    [L]
</IfModule>

但是当我添加以下代码以删除 htaccess 中的 public/index.php 时,它不起作用:-

RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php[^/] /? [L,R=302,NC,NE]

RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php(?:/(.*))?$ /? [L,R=302,NC,NE]

应重定向的示例 url:

  1. mydomain.com/public/index.php/something 应该重定向到 mydomain.com/something(可以是任何东西 - 可以包含任何字符)
  2. mydomain.com/public/index.php 应该重定向到 mydomain.com
  3. mydomain.com/index.php?anything 应该重定向到 mydomain.com?anything(任何内容都可以包含任何字符)

请帮我解决这个问题。

您可以在 public/.htaccess:

中使用此规则
RewriteEngine On

RewriteCond %{THE_REQUEST} /public/index\.php(?:[/?](\S+))?\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]

RewriteCond %{THE_REQUEST} /public/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]

# other rules below this line

另外在站点根目录 .htaccess 中使用此代码:

RewriteEngine On

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

RewriteRule ^ public%{REQUEST_URI} [L]