0

我正在尝试通过 .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>
4

1 回答 1

0

您的重定向语句是 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-07-23T17:38:59.383 回答