重定向问题和htaccess

时间:2014-12-24 04:08:36

标签: .htaccess redirect

我有重定向问题。我有我的htaccess设置来执行以下操作...  1.从URL中删除PHP扩展  2.添加自定义错误页面  3.强制执行301重定向

但重定向存在严重问题...... 如果用户去... www.mysite.com/articles/jewelry/types.php ...然后,基于第二301重定向,他们应该被发送到...... www.mysite.com/article/medieval-jewelry-types-and-functions/ ......然而,他们被重定向到...... www.mysite.com/article/medieval-jewelry/types

如果他们去...... www.mysite.com/articles/picks/A_1.php ......他们应该被重定向到...... www.mysite.com/article/picks-tools/ ......但他们被送到...... www.mysite.com/article/how-to-pick /

看起来重定向正在改变网址的一部分...可能是因为.php扩展名(但这只是推测 - 因为我对编辑htaccess文件知之甚少)。有什么想法吗?

以下是我的htaccess文件的大部分内容,并对可能导致问题的内容进行了一些评论。知道怎么解决吗?

# enable the rewrite engine
RewriteEngine On
# Set your root directory
  RewriteBase /

# remove the .php extension
  RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
  RewriteRule (.*)\.php$ $1 [R=301]

# remove index and reference the directory
  RewriteRule (.*)/index$ $1/ [R=301]

# remove trailing slash if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*)/ $1 [R=301]

# forward request to html file, **but don't redirect (bot friendly)**
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule (.*) $1\.php [L]

ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php 

# example of redirects
Redirect 301 /articles/index.php /articles/

# it looks like the redirect below is conflicting with...
Redirect 301 /articles/jewelry/ /article/medieval-jewelry/
# ... this redirect.
Redirect 301 /articles/jewelry/types.php /article/medieval-jewelry-types-and-functions/

# And this redirect...
Redirect 301 /articles/picks/ /article/how-to-pick/
Redirect 301 /articles/picks/index.php /article/how-to-pick/
# ... is conflicting with this one...
Redirect 301 /articles/picks/A_1.php /article/picks-tools/ 

1 个答案:

答案 0 :(得分:0)

问题似乎是错误的规则顺序,错误的正则表达式以及mod_rewrite规则与Redirectmod_alias)规则的混合。试试这个:

ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php 

# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /

# example of redirects
RewriteRule ^articles/index\.php$ /articles/ [L,NC,R=302]

# it looks like the redirect below is conflicting with...
RewriteRule ^articles/jewelry/?$ /article/medieval-jewelry/ [L,NC,R=302]

RewriteRule ^articles/jewelry/types\.php$ /article/medieval-jewelry-types-and-functions/  [L,NC,R=302]

RewriteRule ^articles/picks/?$ /article/how-to-pick/ [L,NC,R=302]

RewriteRule ^articles/picks/index\.php$ /article/how-to-pick/ [L,NC,R=302]

RewriteRule ^articles/picks/A_1\.php$ /article/picks-tools/ [L,NC,R=302]

# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]

# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# forward request to php file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]