使用301重定向(.htaccess)或不同的解决方案不更改网址

时间:2017-07-24 16:34:06

标签: .htaccess codeigniter mod-rewrite mod-alias

我在网站上更改了一些网址,所以现在我需要将旧网址(由于外部链接)重定向到新网址。

我尝试在.htaccess文件中使用这样的内容:

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

重定向有效(它会打开正确的页面),但问题是此页面的网址更改为以下内容:

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 个答案:

答案 0 :(得分:1)

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

因此,在现有指令之前,请尝试以下内容:

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

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

只有当您确定它正常工作时才将302(临时)更改为301(永久) - 以避免浏览器缓存错误的重定向。

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