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

Using 301 redirect (.htaccess) or a different solution to not change the url

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

我试图在我的 .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  !^(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?/ [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>

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

因此,在现有指令之前,请尝试如下操作:

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

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

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

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