2017-07-24 81 views
2

我改变了我的网站上的一些网址,所以现在我需要将旧网址(由于外部链接)重定向到新网址。使用301重定向(.htaccess)或不同的解决方案,以不更改网址

我试图用这样的事情在我.htaccess文件:

Redirect 301 /pt/oldpage https://www.example.com/pt/newpage 

重定向作品(它会打开正确的页面),但问题是,这个网页的URL被改变这样的事情:

https://www.example.com/pt/newpage?/pt/oldpage ~ 

任何想法是什么错?

我的网站使用FrameWork CodeIgniter,如果这是必要的。

这是我.htaccess文件(通过该解决方案!):

RewriteEngine on 
RewriteRule ^pt/oldpage$ https://www.example.com/pt/newpage? [R=302,L] 
### WWW & HTTPS 

# ensure www. 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# ensure https 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

### WWW & HTTPS 
RewriteCond $1 !^(index\.php|images|assets|recursos|support|robots\.txt|sitemap\.xml|sitemap\.html) 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ 
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ 
RewriteRule ^(.*)$ index.php?/$1 [L] 

Redirect 301 /pt/oldpage https://www.example.com/pt/newpage 
RewriteRule ^pt/oldpage$ /pt/newpage? [R=302,L] 

Options -Indexes 

<IfModule mod_expires.c> 
    ExpiresActive on 
    ExpiresByType image/jpg "access 1 month" 
    ExpiresByType image/gif "access 1 month" 
    ExpiresByType image/jpeg "access 1 month" 
    ExpiresByType image/png "access 1 month" 
    ExpiresByType text/css "access 1 month" 
    ExpiresByType application/x-javascript "access plus 1 month" 
    ExpiresByType text/javascript "access plus 1 month" 
    ExpiresByType application/javascript "access plus 1 month" 
    ExpiresByType image/x-icon "access plus 1 month" 
    ExpiresByType image/icon "access plus 1 month" 
    ExpiresByType application/x-ico "access plus 1 month" 
    ExpiresByType application/ico "access plus 1 month" 
    ExpiresByType application/pdf "access 1 month" 
    ExpiresByType text/x-javascript "access 1 month" 
    ExpiresByType application/x-shockwave-flash "access 1 month" 
    ExpiresByType image/x-icon "access 1 year" 
    ExpiresByType text/html "access 1 month" 
    ExpiresDefault "access 1 month" 
</IfModule> 


################################## 
#        # 
# Google Page speed optimizations# 
#        # 
################################## 

#Enable compression 

<IfModule mod_deflate.c> 
    # Compress HTML, CSS, JavaScript, Text, XML and fonts 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font 
    AddOutputFilterByType DEFLATE application/x-font-opentype 
    AddOutputFilterByType DEFLATE application/x-font-otf 
    AddOutputFilterByType DEFLATE application/x-font-truetype 
    AddOutputFilterByType DEFLATE application/x-font-ttf 
    AddOutputFilterByType DEFLATE application/x-javascript 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE font/opentype 
    AddOutputFilterByType DEFLATE font/otf 
    AddOutputFilterByType DEFLATE font/ttf 
    AddOutputFilterByType DEFLATE image/svg+xml 
    AddOutputFilterByType DEFLATE image/x-icon 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/javascript 
    AddOutputFilterByType DEFLATE text/plain 
    AddOutputFilterByType DEFLATE text/xml 

    # Remove browser bugs (only needed for really old browsers) 
    BrowserMatch ^Mozilla/4 gzip-only-text/html 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
    Header append Vary User-Agent 
</IfModule> 

回答

1

Codeignitor使用了mod_rewrite为它的前端控制器。你将需要使用相同的重定向,而不是使用mod_alias Redirect(这将无条件执行,导致一些奇怪的重定向 - 我怀疑这是发生在这里)。

所以,现有的指令之前,你可以试试,而不是执行以下操作:

RewriteRule ^pt/oldpage$ https://www.example.com/pt/newpage? [R=302,L] 

替代尾随?将删除请求的任何查询字符串(如果需要)。

只有在确定工作正常后才能更改302(临时)至301(永久) - 避免浏览器缓存错误的重定向。

确保在测试之前清除浏览器缓存。

+0

感谢您的回复。我不知道我是否明白所做的是什么,但我尝试了以下方法: - 我将您的代码替换为您共享的代码,什么都没发生 - 我之前插入了您的代码,并且在我重定向之后,我刚刚得到和以前一样的结果 –

+2

听起来你做得对,但是这应该做成_something_。如果你把你的整个'.htaccess'文件添加到你的问题中,我们可以看看。 – MrWhite

+0

我使用我的.htaccess编辑我的文章 –