从 url 中删除/重定向 public/index.php 以防止重复的 url

kun*_*nal 1 apache .htaccess mod-rewrite url-rewriting laravel

您好,我面临从网址中删除 public/index.php 的问题。从 url 中删除/重定向 index.php 以防止重复的 url 此链接确实帮助我删除了 index.php 表单 url,但我无法从 url 中删除 public/index.php。这是我的下面的 htacces 代码

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

    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [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/$1    [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

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

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

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

应重定向的示例网址:

  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(任何内容都可以包含任何字符)

请帮助我如何解决这个问题。

anu*_*ava 6

你可以在里面使用这个规则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
Run Code Online (Sandbox Code Playgroud)

另外,在站点根目录 .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]
Run Code Online (Sandbox Code Playgroud)