【问题标题】:301 redirection in .htaccess file not working.htaccess 文件中的 301 重定向不起作用
【发布时间】:2013-07-24 01:38:59
【问题描述】:

我正在尝试通过 .htaccess 文件重定向内部链接,但它不起作用。下面是我的 .htaccess 文件“# Redirects”。

如何更好地构建代码以允许有效的重定向链接?

# Use PHP 5.3

AddType application/x-httpd-php53 .php 

# Redirect www to non-www

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# Redirects

Redirect 301 /oldpage http://domain/newpage

# Start Hotlink Protection

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

# EXPIRES CACHING

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/js "access 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"
ExpiresDefault "access 2 days"
</IfModule>

【问题讨论】:

  • 究竟是什么不工作,这条规则Redirect 301 /oldpage http://domain/newpage
  • 确保: (1) mod_alias 已启用。 (2)您的浏览器没有“记住”以前的重定向(在尝试使重定向工作时使用临时重定向,并且只有在一切正常时才将其更改为 301(永久)!)(3)确保您是使用 the docs 中指定的重定向

标签: .htaccess redirect


【解决方案1】:

您的重定向语句是 mod_alias 的一部分,但您拥有的所有其余重写规则都属于 mod_rewrite。这两个模块在处理管道的不同点处理请求,因此如果您同时使用它们,有时它们不会很好地相互配合。在这种情况下,您应该只使用 mod_rewrite 以及 将重定向规则放在您的 wordpress 路由规则之前。这是因为 wordpress 会路由你真正想要重定向的内容。

所以:

# Use PHP 5.3

AddType application/x-httpd-php53 .php 

# Redirect www to non-www

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#### INSERT THE REDIRECTS HERE
# Redirects

RewriteRule ^oldpage(.*)$ http://domain/newpage$1 [L,R=301]


# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

### Remove the redirect from here

# Start Hotlink Protection

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

然后是 htaccess 文件的其余部分

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 2017-06-30
    • 2014-07-05
    • 2014-12-21
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多