【问题标题】:Using 301 redirect (.htaccess) or a different solution to not change the url使用 301 重定向 (.htaccess) 或其他解决方案不更改 url
【发布时间】:2017-07-24 16:34:05
【问题描述】:

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

我尝试在我的.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>

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

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

    【讨论】:

    • 感谢您的回复。我不确定我是否确实理解了应该做什么,但我尝试了以下操作: - 我用你共享的代码替换了我的代码,没有发生任何事情 - 我在重定向之前和之后插入了你的代码,我刚刚得到结果和以前一样
    • 听起来你做了正确的事,但应该这样做某事。如果您将整个 .htaccess 文件添加到您的问题中,我们可以查看。
    • 此代码替换了现有的Redirect 指令。也不要包含Redirect 指令——这只会导致问题。但是,您似乎将此重定向放置在错误的位置......这需要“在现有指令之前”。事实上,这应该紧跟在文件顶部的RewriteEngine 指令之后。我已经更新了我的答案(包括完整的 URL - 在这种情况下是可取的)。
    猜你喜欢
    • 2012-11-20
    • 2016-11-19
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2012-06-19
    • 2014-06-23
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多