【问题标题】:Removing index.php from url with .htaccess file. The code is in laravel framework使用 .htaccess 文件从 url 中删除 index.php。代码在 laravel 框架中
【发布时间】:2017-05-19 15:06:55
【问题描述】:

laravel 框架中的 index.php 通常在 /public_html/public/ 文件夹中。 就我而言,它位于 /public_html 文件夹中

这是我的 .htaccess 文件中的代码

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

我应该做些什么改变来从 url 中排除 index.php ?

代码结构如下:

/public_html/sitemap.xml

/public_html/sitemap.htm

/public_html/server.php

/public_html/robots.txt

/public_html/readme.md

/public_html/index.php

/public_html/.htaccess

/public_html/public

/public_html/数据库

/public_html/config

/public_html/cgi-bin

/public_html/bootstrap

/public_html/app


在 /public_html/public 里面


/public_html/public/web.config

/public_html/public/robots.txt

/public_html/public/favicon.ico

/public_html/public/上传

/public_html/public/js

/public_html/public/img

/public_html/public/css

【问题讨论】:

  • 不太精通 laravel(还),但我相信在 app.config 文件中,您可以设置应用程序的基本文件夹,从而避免所有 .htaccess 箍
  • 如果你进入你的主页,你会看到你没有像homepage.com/index.php那样得到它,而只是主页。你不需要改变任何东西。
  • 您好!问题是谷歌已经索引了一些页面,它保存在搜索结果中的 url 是 homepage.com/index.php/company/punjab-communications-complaints/4165 我需要用 .htaccess 解决这个问题。有什么线索吗?

标签: .htaccess laravel url mod-rewrite hide


【解决方案1】:

我强烈反对将 index.phppublic 文件夹中移出。 通过遵循this 教程,我能够实现您想要做的事情。

在那里,您将代码库从public_html 文件夹移出到/appications/your-app 文件夹,并将public 文件夹的内容移到public_html/your-app

教程一目了然。

【讨论】:

    【解决方案2】:

    我也使用这个 Laravel 项目的结构(项目文件夹中的 index.php)。就我而言,我接下来做了:

    1. 将根目录下的server.php重命名为index.php
    2. 在根文件夹中创建 .htaccess:

    在 IfModule mod_rewrite.c 中插入这段代码

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} !(\.css|\.ttf|\.woff|\.woff2|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|js|images|fonts)/(.*)$ public/$1/$2 [L,NC]
    

    【讨论】:

      【解决方案3】:

      花了几个小时后,我为我编写了以下代码,并且它 100% 正常工作。

      之后使用此代码

      重写引擎开启

      将 index.php 重定向到非 index.php

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

      【讨论】:

        猜你喜欢
        • 2013-02-27
        • 1970-01-01
        • 2021-10-02
        • 2015-01-20
        • 2023-03-07
        • 1970-01-01
        • 2012-12-30
        • 2011-05-20
        • 2017-03-15
        相关资源
        最近更新 更多