【问题标题】:Remove/Redirect public/index.php from url to prevent duplicate urls从 url 中删除/重定向 public/index.php 以防止重复的 url
【发布时间】:2021-05-10 10:08:23
【问题描述】:

您好,我面临从 url 中删除 public/index.php 的问题。 Remove/Redirect index.php from url to prevent duplicate urls 此链接确实帮助我删除 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>

但是当我添加以下代码以删除 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]

应重定向的示例网址:

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

请帮我解决这个问题。

【问题讨论】:

  • 在你的第四个条件mydomain.com/public/index.phpanything should be redirected to mydomain.com anything can contain any characters) 你在浏览器中点击mydomain.com 吗?请确认一次。
  • mydomain.com 只是出于安全目的的示例,我不在这里共享域名
  • @RavinderSingh13 我已经删除了第四个条件
  • public/ 内也有 .htaccess 吗?
  • 是的@anubhava 它在那里

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


【解决方案1】:

您可以在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]

【讨论】:

  • 非常感谢它工作得很好.....我在内部网址 mydomain.com/public/index.php/testing 转换为 mydomain.com//testing 时遇到了一个问题域后的双斜杠
  • 我在公共 htaccess 文件中添加了代码
  • 它还在继续@anubhava
  • 它不会再出现了,你可能有旧的浏览器缓存。使用mydomain.com/public/index.php/qwert1234进行测试
  • 非常非常感谢 :) 你今天让我很开心 :)
猜你喜欢
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2014-03-08
  • 2015-03-19
  • 2020-08-12
  • 1970-01-01
相关资源
最近更新 更多