从旧域重定向后显示htaccess /index.php扩展名

时间:2015-05-29 16:16:04

标签: apache .htaccess mod-rewrite redirect

我发生了重定向问题,到目前为止,我已将所有非www重定向到www,并将旧域重定向到新域。除非从旧域重定向,否则我还会删除有效的/index.php扩展名。理想情况下,我希望不会发生这种情况。这对我来说真是一个黑暗的镜头,因为我不明白这意味着什么(htaccess代码),但我到目前为止大部分工作。

我想做什么: 我最终试图删除所有.php个扩展程序(旧域上的某些页面使用.html作为扩展名,它们现在是.php个页面。所以有重定向那些以及那些)并将所有内容路由到新的www.domain.com(不是domain.com)。

是否有特定的命令来执行重定向?或者我完全错过了一些东西。 .htaccess不是我强大的套件。另外,我应该将它们分开<IfModules>吗? (他们目前是,我在这篇文章中将它们合并为一个以减少代码)

<IfModule mod_rewrite.c>

    DirectoryIndex index.php
    AddDefaultCharset utf-8
    Options +FollowSymlinks -MultiViews -Indexes

    RewriteEngine On
    RewriteBase /

    # redirect to new domain
    RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]

    # html > php
    RewriteRule ^(.*)\.html$ $1.php [L,R]

    #not sure what this does, but was already here, so I'm leaving it... site does not use ssl.
    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]


    # force www
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{SERVER_ADDR} !=127.0.0.1
    RewriteCond %{SERVER_ADDR} !=::1
    RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


    # remove /index.php
    RewriteRule ^index.php/(.*)$ /$1 [R=302,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ index.php?/$1 [L]


</IfModule>

1 个答案:

答案 0 :(得分:1)

请试试以下内容。我已经改变了一些东西并为你简化了它们。评论中的描述。

DirectoryIndex index.php
AddDefaultCharset utf-8

<IfModule mod_rewrite.c>

    Options +FollowSymlinks -MultiViews -Indexes

    # Fire up the engines
    RewriteEngine On

    # The base is not necessarily needed. Enable it if you think it might be needed.
    #
    # RewriteBase /

    # Redirect to new domain (self-explanatory)
    RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

    # Force www. on newdomain.com (plain method)
    # If you want to prevent this when using localhost/127.0.0.1/::1,
    # then uncomment the next two lines just above the rule.
    RewriteCond %{HTTP_HOST} ^newdomain.com [NC]
    # RewriteCond %{SERVER_ADDR} !=localhost
    # RewriteCond %{SERVER_ADDR} !=127.0.0.1
    # RewriteCond %{SERVER_ADDR} !=::1
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

    # Remove "index.php"
    RewriteRule ^index.php(/.*)?$ $1 [R=302,L]

    # Redirect *.html to *.php
    RewriteRule ^(.*).html$ $1.php [R=302,L]

    # If the request does not match a file/directory,
    # internally map it as a QUERY_STRING to index.php.
    # As we're using a query string, might be best to allow
    # new query strings as well.
    #
    # If you would rather use REQUEST_URI, then use the commented
    # rule at the bottom and make sure to comment out the first one.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    # RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

关于302重定向 - 如果你感到高兴并希望它们是永久性的,请将它们设为301。