2013-07-09 107 views
0

我正在寻找一个“.htaccess”文件解决方案来重写以“./index.html”结尾的请求地址和“./index”仅限于站点根或子文件夹,例如“http://www.mydomain.com/index[.html]”,直至“http://www.mydomain.com/”。我发现了一些关于“index.html”问题的帮助,但是我很难使解决方案适用于“./index”的唯一请求。我使用下面的代码与我在尝试“./index"-only片注释:.htaccess - 隐藏根目录和目录中的“/index.html”和“/ index”

# MOD_REWRITE IN USE 
Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/

# Redirect "index" page requests to either root or directory 

# These first two lines work for "./index.html"... 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L] 

## These three lines don't work, last two are alternates of one another... 
## RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index*\ HTTP/ 
## RewriteRule ^index*$ http://%{HTTP_HOST}/ [R=302,L] 
## RewriteRule ^index*$ $1 [R=302,L] 

# Hide .html extension - additional information 

# To externally redirect /dir/foo.html to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC] 
RewriteRule^%1 [R=301,L] 

# To internally forward /dir/foo to /dir/foo.html 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*?)/?$ $1.html [L] 

(后者部分是用于隐藏名” .html”结尾,如所指出的,并且可以是多余的,但我包括整个脚本,)我不知道Apache服务器的版本,但任何援助将不胜感激。谢谢。

+0

第一个'RewriteRule'以及条件对我来说工作正常。你遇到的问题究竟是什么? –

+0

感谢您的检查。我试图在第一次重写下面的double#后面得到注释代码,以匹配“/ index”和可能的“/index.html”条件,并重写URL第一次重写。否则,如果我只是得到“/ index”条件来匹配并重写为“/”,我不会介意有两个重写条件,但我不能得到任何我试图处理“/ index “条件,根本。 –

回答

1

我已经设法回答我自己的问题,在此页上出现的链接帮助,发布后,结合偏离中心的网页搜索一个特定的定义。以下是两个链接:StackOverflow - Apache .htaccess to redirect index.html to root,...Media Temple Support/Knowledgebase - Using .htaccess rewrite rules - .htaccess。新代码如下所示:

## MOD_REWRITE in use ## 

Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/

## Redirect index page requests to either root or directory ## 

RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/ 
RewriteRule ^(.*)index.*$ "/$1" [R=301,L] 

## Hide .html extension ## 

## To externally redirect /dir/foo.html to /dir/foo... 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC] 
RewriteRule^%1 [R=301,L] 

## To internally forward /dir/foo to /dir/foo.html... 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*?)/?$ $1.html [L] 

新的第一重写/条件可以修改看起来更像原来的第一个,由地方

RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/ 
RewriteRule ^(.*)index.*$ "/$1" [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.*\ HTTP/ 
RewriteRule ^index.*$ http://%{HTTP_HOST}/ [R=301,L] 

但我不知道为什么你可以这样做,因为后者工作得很好,文本较少。也许我想念一些东西 - 但如果它没有坏掉,不要修复它。

我还没有能够确定原来的第一次重写/条件是从哪里来的,但它是按照它的设计目的进行的,尽管我正在寻找一些与众不同的东西。我希望这些乱码对于那些寻求这种解决方案的人来说是有意义的。感谢“Jon Lin”和“Bip”作出回应以帮助他们,但是,在搜索和重新搜索几小时的时候,意外地发现了这一点。现在我的网站像其他网站一样运行。多谢你们!

+0

我应该补充一点,“索引”重定向部分的最终版本也适用于“index.php”,或者任何其他“索引[.whatever]”请求,如果乍一看并不明显。 –

0

检查它here我认为,它是你的问题。 你也可以检查this

+0

这个答案对我来说并不清楚,因为提供的这两个链接都提到Windows服务器操作系统上的IIS配置,而不是Linux服务器上的htaccess配置。不知道你打算在这里。 –