将URL扩展为301,扩展名为none,但在几个目录树级别下

时间:2012-09-19 18:16:13

标签: .htaccess redirect url-rewriting

我找到了这个解决方案,可以成功地将网址从www.domain.com/example.php重定向到www.domain.com/example,但是当存在少量子网时,我不能这样做,例如:domain.com/dir1/di2/dir3/example.phpdomain.com/dir1/di2/dir3/example < / p>

如何修改此规则以使之前的重定向有效?

RewriteRule (.*).php$ /$1 [R=301,L]
RewriteRule (.*).html$ /$1 [R=301,L]

这是我的.htaccess

ErrorDocument 404 /misc/404.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule (.*).php$ /$1 [R=301,L]
RewriteRule (.*).html$ /$1 [R=301,L]

# Forces a trailing slash to be added
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
#RewriteRule (.*)$ /$1/ [R=301,L]

谢谢!

1 个答案:

答案 0 :(得分:1)

  

http://demo.domain.com/en/index.php将我带到http://demo.domain.com/index,它应该带我去http://demo.domain.com/en/index

     

.htaccess位于/ en /目录

您需要添加:

RewriteBase /en/

RewriteEngine on下面。

您还可能需要在重定向规则和条件中添加/en/

RewriteRule (.*).php$ /$1 [R=301,L]
RewriteRule (.*).html$ /$1 [R=301,L]

需要:

RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*).php$ /en/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*).html$ /en/$1 [R=301,L]