【问题标题】:Consolidating .htaccess and fix bug合并 .htaccess 并修复错误
【发布时间】:2016-02-15 20:42:20
【问题描述】:

首先我应该说我不是 .htaccess 的专家,我所做的一切都来自我遵循的教程。

我已经编辑了 Laravel 附带的标准 .htaccess 文件以包含以下行

  • 从 URL 中删除公共
  • 强制 https
  • 将对 www.mysite.com 的请求重定向到 mysite.com

我目前遇到的问题是,当我访问路线 mysite.com/sitemapwww.mysite.com/sitemap 时,它会生成一个新的站点地图,然后重定向到 mysite.com/index.php,从而有效地摆脱 Laravel 旨在修复的漂亮网址.

这是我的公共目录中的.htaccess 文件:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

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

    # Redirect www.mysite.com to mysite.com
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    # Force SSL
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

这是我根目录下的.htaccess文件

# Remove public
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

这是我的routes.php 处理sitemap 路线

Route::get('sitemap', function(){

    // create new sitemap object
    $sitemap = App::make("sitemap");

    // add items to the sitemap (url, date, priority, freq)
    $sitemap->add(URL::to('/'), '2015-11-14T20:10:00+02:00', '1.0', 'monthly');
    $sitemap->add(URL::to('getcv'), '2015-11-14T12:30:00+02:00', '0.9', 'monthly');

    // generate your sitemap (format, filename)
    $sitemap->store('xml', 'sitemap');
    // this will generate file sitemap.xml to your public folder

    return redirect('/');

});

我的问题是,我怎样才能使mysite.com/sitemapwww.mysite.com 仍然遵循我的.htaccess 中的所有规则,同时在遵循sitemap 路线而不是去@ 之后重定向到mysite.com 987654338@?

【问题讨论】:

    标签: apache .htaccess laravel mod-rewrite laravel-5


    【解决方案1】:

    您需要在/public/.htaccess 中重新排序您的规则:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect www.mysite.com to mysite.com
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
        # Force SSL
        RewriteCond %{HTTPS} !=on
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

    确保在测试此更改时清除浏览器缓存。

    【讨论】:

    • 完美,谢谢!我不认为设置有任何问题,但没有足够的使用 .htaccess 的经验来意识到我需要重新排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2019-11-28
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多