htaccess重定向将旧网址附加到新网址

时间:2016-04-15 08:57:49

标签: php apache .htaccess redirect mod-rewrite

网站正在使用表达式引擎,尝试使用以下代码添加一些简单的301重定向;

Redirect 301 /contact-us/ http://www.website.co.uk/get-in-touch

这将返回以下网址;

http://www.website.co.uk/get-in-touch?contact-us 

我找不到为什么这样做,我也试过了;

RewriteRule ^/contact-us/$ http://www.website.co.uk/get-in-touch [R=301]
RedirectMatch 301 ^/contact-us/?$ http://www.website.co.uk/get-in-touch

两者都返回相同的结果,htaccess作为一个整体;

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Removes index.php from ExpressionEngine URLs
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #enforce the www's
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

RewriteRule ^/contact-us/$ http://www.website.co.uk/get-in-touch [R=301]
#RedirectMatch 301 ^/contact-us/?$ http://www.website.co.uk/get-in-touch
#Redirect 301 /contact-us/ http://www.website.co.uk/get-in-touch

1 个答案:

答案 0 :(得分:1)

您的第一个RewriteRule似乎会欺骗您。

订单中的简单开关应该修复它。我的一般经验法则:首先是外部重定向,内部重写第二个。

所以在你的情况下:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect /contact-us to /get-in-touch
    RewriteRule ^contact-us(/?)$ /get-in-touch [L,R=301]

    # Removes index.php from ExpressionEngine URLs
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #enforce the www's
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>