【问题标题】:Laravel 4 htaccess Rewrite Rule with 301Laravel 4 htaccess 用 301 重写规则
【发布时间】:2014-01-27 22:01:39
【问题描述】:

我有以下问题:

我的网站可通过两个域访问:

www.user.tucana.uberspace.de 和 www.my-domain.de

如何创建有效的 301 重定向 www.user.tucana.uberspace.de 到 www.my-domain.de ? 我的意思是,如果用户通过 www.user.tucana.uberspace.de/actual/news 进入我的网站,我希望我的 htaccess 将重定向到 www.my-domain.de/actual/news ...

在我的 htacces 中,我创建了以下代码:

选项 - 多视图 重写引擎开启

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

RewriteBase /
RewriteCond %{HTTP_HOST} !=www.my-domain.de
RewriteRule (.*) http://www.my-domain.de/$1 [R=301,L]

但问题是......如果用户在 www.user.tucana.uberspace.de/actual/news 进入该站点,他将被重定向到 www.my-domain.de/index.php/actual/news

此代码可以使用,但不适用于根 URL:

选项 - 多视图 重写引擎开启

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !=www.my-domain.de [NC]
RewriteRule ^ http://www.my-domain.de%{REQUEST_URI} [R=301,L,NE]

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

如果我使用上面的代码,网址 www.user.tucana.uberspace.de/actual/news 会更改为 www.my-domain.de/actual/news - 这是正确的, 但是,如果我输入 www.user.tucana.uberspace.de,URL 将更改为 www.my-domain.de/index.php ... 为什么 :-( ? 那么,如何避免 url 中的 index.php 呢?

【问题讨论】:

    标签: apache .htaccess mod-rewrite laravel-4 http-status-code-301


    【解决方案1】:

    你可以像这样保持你的规则:

    Options -MultiViews
    
    RewriteEngine On
    
    # remove index.php
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
    
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\.my-domain\.de$ [NC]
    RewriteRule ^ http://www.my-domain.de%{REQUEST_URI} [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    还要在新浏览器中测试一下,以避免 301 缓存。

    【讨论】:

    • 嘿,谢谢。哦,好吧,我认为是缓存 .. 删除缓存后它现在正在工作... örks :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2012-11-07
    • 2011-04-18
    • 2010-12-22
    • 2011-01-05
    相关资源
    最近更新 更多